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

带回溯的递归。(带回溯没法用循环实现)

class Solution {
public List<List<Integer>> combine(int n, int k) {
result = new ArrayList<>();
List<Integer> ans = new ArrayList<Integer>();
dfs(ans,n,k,1);
return result;
} void dfs(List<Integer> ans, int n, int k, int depth){
if(ans.size()==k) {
List<Integer> new_ans = new ArrayList<Integer>(ans);
result.add(new_ans);
return;
}
if(depth>n){
return;
} ans.add(depth);
dfs(ans,n,k,depth+1);
ans.remove(ans.size()-1);
dfs(ans,n,k,depth+1); } private List<List<Integer>> result;
}

77. Combinations (JAVA)的更多相关文章

  1. 77. Combinations (java 求C(n,k)的组合,排除重复元素)

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 解析:同求全 ...

  2. 第77节:Java中的事务和数据库连接池和DBUtiles

    第77节:Java中的事务和数据库连接池和DBUtiles 前言 看哭你,字数:8803,承蒙关照,谢谢朋友点赞! 事务 Transaction事务,什么是事务,事务是包含一组操作,这组操作里面包含许 ...

  3. 77. Combinations

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

  4. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  5. Leetcode 77, Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  6. 77. Combinations(medium, backtrack, 重要, 弄了1小时)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  7. 【一天一道LeetCode】#77. Combinations

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  8. [leetcode]77. Combinations组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  9. (效率低下)77. Combinations C++回溯法 组合

    https://leetcode.com/problems/combinations/ 沿用78题的思路 class Solution { public: void backTrack(vector& ...

随机推荐

  1. java 开发工具包 jdk 64位 jdk-8u221-windows-x64.exe 迅雷下载

    迅雷下载链接(迅雷新建任务,复制链接进去): https://download.oracle.com/otn/java/jdk/8u221-b11/230deb18db3e4014bb8e3e8324 ...

  2. Mysql : Maximum execution time of 30 seconds exceeded

    在向Mysql数据库中插入数据时,提示Maximum execution time of 30 seconds exceeded.......翻译:最大运行时间超过30秒. 最后在php.ini中找到 ...

  3. spark 笔记 8: Stage

    Stage 是一组独立的任务,他们在一个job中执行相同的功能(function),功能的划分是以shuffle为边界的.DAG调度器以拓扑顺序执行同一个Stage中的task. /** * A st ...

  4. leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)

    数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...

  5. kotlin之基本数据类型

    数据类型                       占用字节数 Double 8 Float 4 Long 4 Int 4 Short 2 Byte 1 数据类型之间的转换 toByte():转换为 ...

  6. [flask-sqlalchemy]倒序查询结果

    按创建时间倒序查询测试用例 >>> a= TestCase.query.order_by(TestCase.create_time.desc()).first() >>& ...

  7. ControlTemplate in WPF —— ItemsControl

    <ItemsControl Margin=" ItemsSource="{Binding Source={StaticResource myTodoList}}"& ...

  8. 阶段3 2.Spring_03.Spring的 IOC 和 DI_12 注入集合数据

    再复制一份,改名3 常用的注入方式,这里选择set saveAccount方法输出所有的对象 map需要导包 配置xml 集合类型的值配置方式 在property标签里面再写标签 这里选择array. ...

  9. CentOS 7.x关闭/开启防火墙出现Unit iptables.service failed to load: No such file or directory问题解决

    一直用CentOS 6.x,今天用CentOS7.3版本时,防火墙配置后执行service iptables start出现”Failed to restart iptables.service: U ...

  10. JavaScript基础修炼(14)

    目录 一. PCM格式是什么 二. 浏览器中的音频采集处理 三. 需求实现 方案1——服务端FFmpeg实现编码 方案2——ScriptProcessorNode手动处理数据流 参考文献 示例代码托管 ...