作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/combinations/description/

题目描述

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],
]

题目大意

找出一个数组的所有可能的组合

解题方法

方法一:递归

这个题要找到组合,组合和排列的不同之处在于组合的数字出现是没有顺序意义的。

剑指offer的做法是找出n个数字中m的数字的组合方法是,把n个数字分成两部分:第一个字符和其他的字符。如果组合中包括第一个字符,那么就在其余字符中找到m-1个组合;如果组合中不包括第一个字符,那么就在其余字符中找到m个字符。所以变成了递归的子问题。

我的做法如下,这个之中用到了if k > len(array)的做法,防止数组过界陷入死循环(其作用主要是对第二个递归而言的)。

class Solution(object):
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
res = []
self.helper(range(1, n + 1), k, res, [])
return res def helper(self, array, k, res, path):
if k > len(array):
return
if k == 0:
res.append(path)
else:
self.helper(array[1:], k - 1, res, path + [array[0]])
self.helper(array[1:], k, res, path)

方法二:回溯法

这样的思想是我们抽取第一个字符,然后从后面n-1个字符中抽出m-1个;抽取第二个字符,再从后面的n-2个字符抽出m-1个……这样循环下去。因为这样的操作每次都是往后进行寻找的,所以不用考虑去重的问题。

class Solution(object):
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
res = []
self.helper(range(1, n + 1), k, res, [])
return res def helper(self, array, k, res, path):
if k > len(array):
return
if k == 0:
res.append(path)
else:
for i in range(len(array)):
self.helper(array[i + 1:], k - 1, res, path + [array[i]])

C++代码如下:

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

日期

2018 年 3 月 11 日
2018 年 12 月 20 日 —— 感冒害的我睡不着

【LeetCode】77. Combinations 解题报告(Python & C++)的更多相关文章

  1. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  2. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  4. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  5. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  6. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  7. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  8. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  9. 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...

随机推荐

  1. DRF请求流程及主要模块分析

    目录 Django中CBV请求生命周期 drf前期准备 1. 在views.py中视图类继承drf的APIView类 2. drf的as_view()方法 drf主要模块分析 1. 请求模块 2. 渲 ...

  2. .Net Core——用SignalR撸个游戏

    之前开内部培训,说到实时web应用这一块讲到了SignalR,我说找时间用它做个游戏玩玩,后面时间紧张就一直没安排.这两天闲了又想起这个事,考虑后决定用2天时间写个斗D主,安排了前端同学写客户端,我写 ...

  3. 【编程思想】【设计模式】【创建模式creational】建造者模式builder

    Python版 https://github.com/faif/python-patterns/blob/master/creational/builder.py #!/usr/bin/python ...

  4. java通过JDBC访问数据库(最基本的查询)

    一.步骤介绍 1.通过Class.forName()加载驱动: 2.通过DriverManager.getConnection()获取Conncetion连接对象: 3.创建Statement对象传递 ...

  5. 深度解析Spring Cloud Ribbon的实现源码及原理

    Ribbon的核心作用就是进行请求的负载均衡,它的基本原理如下图所示.就是客户端集成Ribbon这个组件,Ribbon中会针对已经配置的服务提供者地址列表进行负载均衡的计算,得到一个目标地址之后,再发 ...

  6. 沉淀vue相关知识(主要还是个人积累用)

    路由懒加载的配置: const Home= () =>import('../components/Home') //使用ES6中的路由懒加载的方式 const About= () =>im ...

  7. Java变量和常量

    变量 变量要素包括:变量名,变量类型,作用域. 变量作用域:类变量(static),实例变量(没有static),局部变量(写在方法中) //类中可以定义属性(变量) static double sa ...

  8. pipeline 共享库

    目录 一.简介 二.共享库扩展 共享库使用 共享库结构 pipeline模板 一些小问题 三.共享库例子 使用公共变量 使用共享库的src方法 使用共享库的vars方法 四.插件实现pipeline ...

  9. 如何查看Python的版本号

    一.如何查看Python的版本号 win+r输入cmd在输入:python --version回车即可

  10. DIgSILENT PowerFactory 15.1.7 破解过程

    将dll文件复制到安装路径下: