题目:

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. html中元素的id和name的区别(2016-1-22)

    HTML中元素的Id和Name属性区别 一直以来一直以为在html中,name和id没什么区别,今天遇到一个坑才发现(PHP获取不到表单数据,原因:元素没有name,只定义了id),这两者差别还是很大 ...

  2. 深度学习基础系列(五)| 深入理解交叉熵函数及其在tensorflow和keras中的实现

    在统计学中,损失函数是一种衡量损失和错误(这种损失与“错误地”估计有关,如费用或者设备的损失)程度的函数.假设某样本的实际输出为a,而预计的输出为y,则y与a之间存在偏差,深度学习的目的即是通过不断地 ...

  3. Error after SQL Server 2012 installation: Login Failure for "SQL Server Integration Services 11.0" SSIS service

    When you install SQL Server 2012 and you try to connect to SSIS services, you cannot due to that the ...

  4. ARM 内核

    ARM相关知识: ARM核:A8,ARM11,ARM9 指令架构:ARMv7,ARMv6,ARMv4 ARM核分为两个阵营: 经典型:ARM7,ARM9,ARM11 Cortex: Cortex A: ...

  5. Django框架(一)-Django初识

    Django初识 一.Web框架本质—自己实现Web框架 1.所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端 import socket sk = sock ...

  6. php异步实现,避免长时间等待

    处理的php异步的方法有好几种,这里我就只介绍我经常用的而且官方也推荐的 废话少说,直接贴代码 //php异步 public function doRequest($host,$path, $para ...

  7. 【平面图最小割】BZOJ2007-[NOI2010]海拔

    [题目大意] 城市被东西向和南北向的主干道划分为n×n个区域,包括(n+1)×(n+1)个交叉路口和2n×(n+1)条双向道路.现得到了每天每条道路两个方向的人流量.每一个交叉路口都有海拔,每向上爬h ...

  8. C++之lambda理解

    简介 在C++ Primer中,是这样定义的-一个lambda表达式表示一个可调用的代码单元,可以将其理解为一个未命名的内联函数:与任何函数类似,一个lambda具有一个返回类型,一个参数列表和一个函 ...

  9. BZOJ 4521 CQOI 2016 手机号码 数位DP

    4521: [Cqoi2016]手机号码 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 539  Solved: 325[Submit][Status ...

  10. SCC缩点

    int V; //顶点数量 vector<int> G[max_v]; //图的邻接表表示方法 vector<int> rG[max_v]; //把边反向建的图 vector& ...