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

Example:

Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
] Time: O(C(N, k))
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
res = []
start = 0
my_list = []
self.dfs(n, k, 1, my_list, res)
return res def dfs(self, n, k, start, my_list, res):
if k == 0:
res.append(list(my_list))
return
for i in range(start, n + 1):
my_list.append(i)
# need to use i + 1 instead of start + 1 for the next level
self.dfs(n, k - 1, i + 1, my_list, res)
my_list.pop()
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
helper(res, new ArrayList<Integer>(), n, k, 1);
return res;
} private void helper(List<List<Integer>> res, List<Integer> list, int n, int k, int start) {
if (k == 0) {
res.add(new ArrayList<>(list));
return;
}
// need to go deeper based on current i and ignore the previous result, so that use i + 1
for (int i = start; i <= n; i++) {
list.add(i);
helper(res, list, n, k - 1, i + 1);
list.remove(list.size() - 1);
}
}
}

[LC] 77. Combinations的更多相关文章

  1. Leetcode 77, Combinations

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

  2. 77. Combinations

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

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

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

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

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

  5. [leetcode]77. Combinations组合

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

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

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

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

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

  8. LeetCode OJ 77. Combinations

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

  9. LeetCode 77 Combinations(排列组合)

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

随机推荐

  1. Vue.js(4)- 生命周期

    当new的时候,就创建了一个vue实例,这个实例就是vue框架的入口,也是VM层 <!DOCTYPE html> <html lang="en"> < ...

  2. 动态改变tableHeaderView的显示隐藏及高度

    改变tableHeaderView的高度: UIView *headerView = _tableView.tableHeaderView; headerView.height = 10; 当设置高度 ...

  3. WGAN将数值限制在一定范围内 Python代码 tf.clip_by_value(p, -0.01, 0.01))

    tf.clip_by_value(p, min, max))   运用的是交叉熵而不是二次代价函数. 功能:可以将一个张量中的数值限制在(min,max)内.(可以避免一些运算错误:可以保证在进行lo ...

  4. python刷LeetCode:3.无重复字符的最长子串

    难度等级:中等 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 ...

  5. VNC/XRDP/XDMCP尝试

    (记得安装X Window System等 可参考链接https://www.linuxidc.com/Linux/2017-10/147646.htm) 看本文档时可以参考 https://blog ...

  6. Python—程序设计:抽象工厂模式

    抽象工厂模式 内容:定义一个工厂类接口,让工厂子类来创建一系列相关或相互依赖的对象. 例:生产一部手机,需要手机壳.CPU.操作系统三类对象进行组装,其中每类对象都有不同的种类.对每个具体工厂,分别生 ...

  7. 在项目中ES6语法转ES5语法 babel转码器

    es6 babel 安装以及使用   1,安装好node(需要使用npm包管理工具) 2,在本地项目路径下npm init,格式化成功后会在项目下生成一个配置文件package.json 3,本地安装 ...

  8. MobX 在 hook 中的使用

    关于 mobX 在 react 16.8.0 以上的用法 以下例子均取自官网文档 一般用法: import { observer, useLocalStore } from 'mobx-react'; ...

  9. Asp.Net MVC主项目访问不到分离项目控制器的解决方案

    我在portal主项目外新建一个分离项目,控制器和Model都写在分离项目中,视图层写在portal中. 我更改了命名空间,引用了Dll,还是不能访问到控制器. 找到问题: 最后我发现是主项目port ...

  10. Python实现自动处理表格,让你拥有更多的自由时间!

    相信有不少朋友日常工作会用到 Excel 处理各式表格文件,更有甚者可能要花大把时间来做繁琐耗时的表格整理工作.最近有朋友问可否编程来减轻表格整理工作量,今儿我们就通过实例来实现 Python 对表格 ...