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期末课程总结

    期末课程总结 转眼间,这个学期就要过去了,我们Java的学习也接近了尾声,回想到这个学期刚开始接触到Java的时候,感觉什么都不懂,但现在似乎有了门路,不会载懵懵懂懂, 虽然本学期面向对象与Java程 ...

  2. 网络1911、1912 C语言第2次作业--循环结构 批改总结

    一.评分规则 伪代码务必是文字+代码描述,直接反应代码,每题扣1分 提交列表没内容,或者太简单,每题得分0分.注意选择提交列表长的题目介绍. 代码格式不规范,继续扣分. 代码互评,内容简单,0分. 原 ...

  3. UIButton设置按钮点击范围大于可视范围

    自定义按钮类型CustomButton,继承UIButton,重写pointInside函数改变点击响应范围. 例如,按钮点击范围比实际高度上下增加6. CustomButton.h @interfa ...

  4. JVM系列3:JVM垃圾回收

    1.JVM内存分配和回收 1.1 对象分配原则 在JVM系列1:内存区域中我们谈到,JVM堆中的内存划分如下: 从中可以看出堆内存分为新生代和老年代以及永久代(在JDK1.8中已经被MetaSpace ...

  5. 阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解

    @Component spring容器是一个Map结构,是由于key 和vlaue组成的 运行测试 无法运行 出错的原因↓ 第一部是解析配置文件.但是配置文件这里是空的.我们的bean里面什么对象都没 ...

  6. 阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备

    适应配置的方式解决我们刚才的编码操作 -dist结尾的就是spring 的开发包 解压好的 这里面是约束 libs是扎包 三个为一组,实际上只有21个 自己在使用需要导入jar包的时候,选择这种没有任 ...

  7. 使用Python创建AI比你想象的轻松

    使用 Python 创建 AI 比你想象的轻松 可能对AI领域,主要开发阶段,成就,结果和产品使用感兴趣.有数百个免费源和教程描述使用Python的AI.但是,没有必要浪费你的时间看他们.这里是一个详 ...

  8. python3下import MySQLdb出错问题

    原因:python2下是使用的MySQLdb,python3下用的是pymysql 安装 pip install pymysql

  9. logstash 写入数据到elasticsearch 索引相差8小时解决办法

    问题说明 Logstash用的UTC时间, logstash在按每天输出到elasticsearch时,因为时区使用utc,造成每天8:00才创建当天索引,而8:00以前数据则输出到昨天的索引 # 使 ...

  10. P4878 [USACO05DEC] 布局

    题面lalala 这居然是个紫题???原谅我觉得这题是模板... 这个这个,这题的算法呢其实是一个叫差分约束的东西,也是今天下午我们机房的重点,如果不知道这个差分约束是个啥的人呢,自行百度一下谢谢.. ...