77. Combinations
来源:互联网 发布:js 设置dialog button 编辑:程序博客网 时间:2024/05/21 07:50
Given two integers n and k, return all possible combinations ofk 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],]
思路: 回溯法
<span style="font-size:14px;">public class Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> res=new ArrayList<List<Integer>>(); combineHelp(1,n,k,new ArrayList<Integer>(),res); return res; } public void combineHelp(int start,int n,int k,List<Integer> list,List<List<Integer>> res){ if(k==0){ res.add(new ArrayList(list)); return; } for(;start<=n;start++){ list.add(start); combineHelp(start+1,n,k-1,list,res); list.remove(list.size()-1); } }}</span>
0 0
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 第七周项目1——建立顺序环形队列算法库
- 2016-10-09课后练习
- Linux经典书籍推荐
- 对象的创建(字面量形式)
- STM32系统时钟设置详解
- 77. Combinations
- nodejs使用async来进行优化
- JavaScript 事件处理
- C语言复习总结(3)
- svn主干与分支学习
- Web 应用程序状态管理
- Reverse Integer
- oracle中exp,imp的使用详解
- 深入了解memcache