题目:

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],
]

题解:

这道题就是用DFS(参考Work BreakII)的循环递归处理子问题的方法解决。n为循环的次数,k为每次尝试的不同数字。用到了回溯。

代码如下:

 1     public ArrayList<ArrayList<Integer>> combine(int n, int k) {
 2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
 3         if(n <= 0||n < k)
 4             return res;
 5         ArrayList<Integer> item = new ArrayList<Integer>();    
 6         dfs(n,k,1,item, res);//because it need to begin from 1
 7         return res;
 8     }
 9     private void dfs(int n, int k, int start, ArrayList<Integer> item, ArrayList<ArrayList<Integer>> res){
         if(item.size()==k){
             res.add(new ArrayList<Integer>(item));//because item is ArrayList<T> so it will not disappear from stack to stack
             return;
         }
         for(int i=start;i<=n;i++){
             item.add(i);
             dfs(n,k,i+1,item,res);
             item.remove(item.size()-1);
         }
     }

Reference:http://blog.csdn.net/linhuanmars/article/details/21260217

Combinations leetcode java的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. Letter Combinations of a Phone Number leetcode java

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  3. [LeetCode][Java] Combinations

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  4. [LeetCode][Java] Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  5. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  6. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  7. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  8. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  9. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. QT防止程序启动两次的方法

    为了使QT 能保证只创建一个实例来进行, 对windows和linux分别采取了全局互斥变量和文件锁的方法. Q_OS_WIN32宏用来表示编译运行的目标平台是windows,Q_OS_LINUX则标 ...

  2. linux网络管理----远程登录工具

    1.对称加密 例子:压缩文件加密码,别人要打开,只能知道你的密码,这样的方法不安全,因为这个密码可能是你的qq密码或者是邮箱密码等等 2.非对称加密 类似于放羽毛球的桶,两边都可以拿资源,两边都加一个 ...

  3. 洛谷P3527 [POI2011]MET-Meteors [整体二分]

    题目传送门 Meteors 格式难调,题面就不妨放了. 分析: 一道整体二分的练手题. 就是一般的整体二分的套路,但是要注意,将修改和询问加入队列的时候要先加修改再加询问.另外,博主代码打得太丑,常数 ...

  4. spring boot上传文件错误The temporary upload location [/tmp/tomcat.5260880110861696164.8090/work/Tomcat/localhost/ROOT] is not valid

    参考了:https://www.jianshu.com/p/cfbbc0bb0b84 再次感谢,但还是有些调整 一.在zuul服务中加入两个配置参数(location: /data/apps/temp ...

  5. React Native性能优化之可取消的异步操作

    前沿 在前端的项目开发中,异步操作是一个不可获取的,从用户的角度来说,异步操作所带来的体验是美妙的,但有时候也会带来一些性能隐患.比如说:有一个异步请求还没有返回结果,但是页面却关闭了,这时由于异步操 ...

  6. CSUOJ 1979 古怪的行列式

    Description 这几天,子浩君潜心研究线性代数. 行列式的值定义如下: 其中,τ(j1j2...jn)为排列j1j2...jn的逆序数. 子浩君很厉害的,但是头脑经常短路,所以他会按照行列式值 ...

  7. CSU - 2058 跳一跳

    Description 冰弦非常热衷于过气微信小游戏"跳一跳",现在给出了他每次游戏时的一些信息,请你帮他计算一下每局游戏的得分. 跳一跳的游戏规则如下: 玩家操控一个小棋子,在形 ...

  8. idea集成项目管理工具 --- Maven 并且【配置tomcat】

    介绍: 1.项目管理工具 POM    Porject Object Model 2.可以管理项目中的的jar包依赖 3.maven   jar包中央仓库:http://mvnrepository.c ...

  9. shell rename directory

    mv can do two jobs. It can move files or directories It can rename files or directories To just rena ...

  10. Struts2 (中)

    Struts中API介绍 ServletAPI 在使用Struts2的框架的过程中,发现Struts2和Servlet的API是解耦合的. 在实际开发中,经常使用到Servlet的API,比如进行登录 ...