题目:
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],
]
解法1:
回溯法
和常规的回溯法一样,在循环条件可以做一点优化,未优化前

for(int i = start; i <= n; ++i){

优化后

for(int i = start; i <= n - (k -path.size()) + 1; ++i){

以n = 4, k = 2为例
1、在path未加入任何元素时,对应下图中的第一层,这时候我们 i 最多循环到n - (k -path.size()) + 1 = 4 - (2 - 0)+ 1 = 3
因为3,4正好满足path的要求,为两个元素,而循环到4,只剩下一个元素无论如何都不可能再有两个元素出现
2、在path未加入1个元素时,对应下图中的第二层,这时候我们 i 最多循环到n - (k -path.size()) + 1 = 4 - (2 - 1)+ 1 = 4
因为这次我们只需要再加1个元素就好,所以4也是可以的

c++:

class Solution {public:vector<vector<int>> combine(int n, int k) {vector<vector<int>> res;vector<int> path;DFS(n, k, path, res, 1);return res;}void DFS(int n, int k, vector<int>& path,vector<vector<int>>& res,int start){if(path.size() == k){res.push_back(path);return;}for(int i = start; i <= n - (k -path.size()) + 1; ++i){path.push_back(i);DFS(n,k, path, res, i+1);path.pop_back();}}};

java:

class Solution {public List<List<Integer>> combine(int n, int k) {List<List<Integer>>  res = new ArrayList<>();List<Integer> path = new ArrayList<>();DFS(n,k, path, res, 1);return res;}private void DFS(int n,int k, List<Integer> path,List<List<Integer>> res,int start){if(path.size() == k){res.add(new ArrayList<>(path));return;}for(int i = start; i <= n - (k -path.size()) + 1 ; ++i){path.add(i);DFS(n,k, path, res, i+1);path.remove(path.size() - 1);}}}

python:

class Solution(object):def combine(self, n, k):""":type n: int:type k: int:rtype: List[List[int]]"""res = []self.DFS(n,k, [], res, 1)return resdef DFS(self, n,k, path, res,start):if len(path) == k:res.append(path)returnfor i in xrange(start, n - (k - len(path)) + 2):self.DFS(n,k, path + [i], res, i+1)

LeetCode 刷题记录 77. Combinations相关推荐

  1. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  2. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  3. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  4. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  5. LeetCode刷题记录11——290. Word Pattern(easy)

    LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...

  6. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  7. LeetCode刷题记录9——58. Length of Last Word(easy)

    LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...

  8. LeetCode刷题记录8——605. Can Place Flowers(easy)

    LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...

  9. LeetCode刷题记录7——824. Goat Latin(easy)

    LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...

最新文章

  1. leetcode--1:(python)Two Sum
  2. 用python播放音乐_Python实例讲解 -- 定时播放 (闹钟+音乐)
  3. PCL:解决PCL和OpenCV冲突的方法
  4. Xcode中Info.plist文件各个键的作用说明【搜藏】
  5. 转 结构体中字节对齐问题(转载)
  6. 微信开发者工具一打开代码编辑区文件全部不见了
  7. 安卓随机通话记录_Android 通话记录
  8. Exchange 2010 (四) CAS Array部署
  9. python提高运行效率_提高CPU密集型任务执行效率——Python多进程介绍,内附实例代码...
  10. vue3没了$children,如何获取子组件???
  11. Spring.Net的AOP的通知
  12. easyui 全部图标
  13. freeswitch 会议创建命令
  14. JAVA毕业设计高校教学资源共享平台计算机源码+lw文档+系统+调试部署+数据库
  15. python 打开pdf文件_Python读取PDF文件--pdfminer
  16. 基于LabVIEW的WIFI通信人机交互界面设计
  17. 11.4王者荣耀服务器维护中,英雄战迹11号更新维护公告 删档最后一更
  18. python合并excel表格
  19. 谈谈区块链正经的商用场景!
  20. safari浏览器找不到服务器

热门文章

  1. 漫画描述数字电路之时序电路
  2. 2021新泰一中高考成绩查询,山东泰安赫赫有名的4所高中,成绩一目了然,第一名花落谁家?...
  3. js操作数组前面插入,后面插入
  4. DHT11温湿度传感器学习笔记
  5. 弧度制下的面积公式与极坐标面积微元极坐标下的线微元
  6. 尚医通-阿里云OSS、用户认证与就诊人
  7. 霍启刚晒全家出游照,网友们纷纷点赞,郭晶晶教育孩子有一套
  8. 中国工业液压油市场市场发展现状研究与投资趋势分析报告2022-2028年
  9. 索尼的hlg是什么_索尼HLG的拍摄使用方法
  10. 阿里前CEO卫哲:我特别反对无人便利店 凤凰新闻 08-24 10:55 原标题:阿里前CEO卫哲:我特别反对无人便利店 卫哲,32岁就成长为史上最年轻的世界500强中国区总裁,36岁成为阿里巴巴CE