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 NIO学习

    Java NIO(转自:http://www.iteye.com/magazines/132-Java-NIO#585) Java NIO提供了与标准IO不同的IO工作方式: Channels,Buf ...

  2. Python统计学statistics实战

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  3. if-else判断语句

    <1>if-else的使用格式 if 条件: 满足条件时要做的事情1 满足条件时要做的事情2 满足条件时要做的事情3 ...(省略)... else: 不满足条件时要做的事情1 不满足条件 ...

  4. OPCDAAuto的一个坑

    最近项目需要对SCADA系统的下位机采集实时数据,常见做法是两种,一种采用ModBus RTU/TCP协议直接通过支持ModBus的下位机通信,一种是通过OPC规范,使用厂商提供的OPC Server ...

  5. ORACLE官方全托管驱动 Oracle.ManagedDataAccess 12.1.0.1.0

    以前用Oracle的时候,必须得装他臃肿的客户端,网上虽然也有提供直连Oracle的驱动,但也是要收费的,最近Oracle终于开窍了,提供了官方的全托管驱动. 这次是随Oracle ODAC 12c  ...

  6. 编辑器UEditor入门学习

    优点:非常使用的富文本编辑器,对比于之前使用的summernote,比前者多出了更多的字体图标 废话少说,直接步骤: 1.导入资源(全部放在单独的文件下即可,下图为“UEditor”文件夹) 2.引用 ...

  7. runoob_Java 方法

    可变参数 JDK 1.5 开始,Java支持传递同类型的可变参数给一个方法. 方法的可变参数的声明如下所示: typeName... parameterName 在方法声明中,在指定参数类型后加一个省 ...

  8. zabbix使用脚本监控

    参照:http://www.cnblogs.com/zhongkai-27/p/9984597.html

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

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

  10. 【神经网络与深度学习】【VS开发】【CUDA开发】VS2013 配置CUDNN V4 DEMO

    VS2013 配置CUDNN V4 DEMO 众所周知,当前主流深度学习的实现中调用的底层API都是cudnn,自己做项目需要开发深度学习模块时,也需要调用cudnn库,因此熟悉cudnn库是很有必要 ...