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. 选题 Scrum立会报告+燃尽图 06

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/8678 一.小组情况组长:贺敬文组员:彭思雨 王志文 位军营 杨萍队名:胜 ...

  2. JNI崩溃调试

    JNI崩溃了,系统日志会打印堆栈信息,所以第一步就是取日志 adb shell logcat -v threadtime >>d:/log.txt 然后找到日志里面的关键字backtrac ...

  3. 【转】数组指针&指针数组

    转自:https://www.cnblogs.com/mq0036/p/3382732.html 数组指针和指针数组的区别 数组指针(也称行指针)定义 int (*p)[n];()优先级高,首先说明p ...

  4. Windows 10下怎么远程连接 Ubuntu 16.0.4(小白级教程)

    前言: 公司因为用Ruby做开发,所有适用了Ubuntu系统,但是自己笔记本是W10,又不想装双系统,搭建开发环境,便想到倒不如自己远程操控公司电脑,这样在家的时候也可以处理一些问题.故此便有了下面的 ...

  5. 我非要捅穿这 Neutron(三)架构分析与代码实现篇(基于 OpenStack Rocky)

    目录 文章目录 目录 Neutron 的软件架构分析与实现 Neutron Server 启动流程 获取 WSGI Application Core API & Extension API C ...

  6. 阶段3 2.Spring_07.银行转账案例_10 使用动态代理实现事务控制

    回到事物的案例中 我们现在希望用代码比较精简的这个AccountServiceImpl这个类.而不是一堆事物的AccountServiceImpl_OLD这个类 新建BeanFactory类 Acco ...

  7. 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 ...

  8. Smartform给文本绑定值

    点击字段显示器, 然后把字段拖过去即可

  9. Unity中的动画系统和Timeline(4) AvatarMask和IK动画

    AvatarMask(骨骼遮罩) 在前面角色动画的基础上,角色在奔跑过程中捡起一块木头,双手要抱着这块木头.如果使用前面的方法,直接切换动画,那么就只剩下抱木头的动画,其它动画就没了.这时我们要使用下 ...

  10. CentOS 7 卸载 mysql

    查看是否安装 mysql rpm -qa | grep -i mysql yum list install mysql* 卸载 yum方式 yum remove mysql mysql-server ...