leetcode 77. Combinations
来源:互联网 发布:四方所 知乎 编辑:程序博客网 时间:2024/06/15 15:26
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
经典问题,经典解法。
public List<List<Integer>> combine(int n, int k) {List<List<Integer>> res=new ArrayList<List<Integer>>();List<Integer> list=new ArrayList<Integer>();helper(res, list, 1, n, k);return res;}public void helper(List<List<Integer>> res,List<Integer> list,int beginNum,int n,int k){if(list.size()==k){res.add(new ArrayList<>(list));return;}if(beginNum>n){return;}for(int i=beginNum;i<=n;i++){list.add(i);helper(res, list, i+1, n, k);list.remove(list.size()-1);}}大神解法也跟我一样的哦。
阅读全文
0 0
- LeetCode 77. Combinations
- [LeetCode]77.Combinations
- LeetCode --- 77. Combinations
- [Leetcode] 77. Combinations
- [leetcode] 77.Combinations
- leetCode 77.Combinations (组合)
- [leetcode] 77. Combinations
- 77. Combinations LeetCode
- <LeetCode OJ> 77. Combinations
- [LeetCode]77. Combinations
- leetcode 77. Combinations
- LeetCode *** 77. Combinations
- leetcode 77. Combinations
- LeetCode 77. Combinations
- LeetCode-77.Combinations
- LeetCode 77.Combinations
- [LeetCode]problem 77. Combinations
- [LeetCode] 77. Combinations
- 深入理解JVM(十)——晚期(运行期)优化
- 浏览器http的缓存机制
- Java与C++的优劣:谁更胜一筹?
- 1977: Bit-reversal Permutation(递归)
- python篇8_python中的变量和访问权限
- leetcode 77. Combinations
- spring boot中的banner制作
- 从知识付费不同时期演化,看其蜕变之路
- 项目管理工具之maven
- Spring-国际化信息03-容器级的国际化信息资源
- 如何将安装好的英文版的Ubuntu系统改为中文
- 简单错误程序——INF;调试到n==1,sum就inf
- 如何将Java Web项目转换为Java项目?
- 最小生成树之Kruskal算法 Java实现