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. (转载)Tomcat 报错 (The tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config is mi)

    错误如图所示: 目前对于这个错误的原因尚不清楚,目前只知道如何解决这个错误,等到以后知道了原因之后再更改此文. 原因猜测: 之前你的eclipse关联的tomcat由于某种原因出现了信息丢失,需要重新 ...

  2. CYPHER 语句(Neo4j)

    CYPHER 语句(Neo4j) 创建电影关系图 新增 查找 修改 删除 导入 格式转换 创建电影关系图 CREATE (TheMatrix:Movie {title:'The Matrix', re ...

  3. shell中通过sed替换文件中路径

    通常sed指令修改行内容时使用:sed -i " 9 s/^.*/"type in what you want modified!"/" 其中"typ ...

  4. Docker部署zookeeper集群和kafka集群,实现互联

    本文介绍在单机上通过docker部署zookeeper集群和kafka集群的可操作方案. 0.准备工作 创建zk目录,在该目录下创建生成zookeeper集群和kafka集群的yml文件,以及用于在该 ...

  5. Android studio中2种build.gradle文件介绍

    根目录下的build.gradle通常不需要修改这个文件中的内容,除非需要添加一些全局的项目构建配置 buildscript { repositories { google() //声明代码托管仓库G ...

  6. 合并两个django的queryset

    有queryset:A和B 要合并它们,根据网上的答案,貌似是用itertools库的chain对象比较好,地址 c=chain(x,y)   但是当c用于分页的时候,就有问题,会报chain没有le ...

  7. 写一个读取Excel表格的接口

    # -*- coding: gbk -*-import xlrd class Canshu: def __init__(self,filepath): """ 创建文件对 ...

  8. vzray上网教程

    1.首先按照之前的教程在chrome里安装插件-Proxy-SwitchyOmega-Chromium-2.5.15 2.打开  vzray-v3.11-windows-64,打开 3.在chrome ...

  9. 成为优秀Angular开发者所需要学习的19件事

    一款to-do app基本等同于前端开发的"Hello world".虽然涵盖了创建应用程序的CRUD方面,但它通常只涉及那些框架或库也能做到的皮毛而已. Angular看起来似乎 ...

  10. 17.3.12--smtplib模块发送邮件__抄送,安装与下载

    1-----在日常编程中,经常会用到处理email,发送,接收,抄送,下载邮件内容等操作,这时候就需要用Python的smtplib模块 smtplib与Email服务器(server)相互通信来传送 ...