leetcode 之 Combinations

描述:

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],
]
即 排列组合的问题. 对于例子来说,就是C4, 2, 从4个里面取出两个来.之前有做过全排列的问题和求所有子集的问题. 见:   http://www.cnblogs.com/missmzt/p/5527382.html

这里的解决方法都是通过回溯法来解决. 个人对于回溯法还不是很熟练,主要在于回溯时条件函数的设计吧. 例如之前的 全排列问题是 通过一个标记数组, 标记第i个数是否放入结果集合中.对于组合问题, 当回溯判断 集合的长度达到k时, 这时应该返回上一层, 开始回溯. 在回溯的过程中,我们设定开始的数和结束的数.代码如下:
 1 class Solution(object):
 2     def combine(self, n, k):
 3         """
 4         :type n: int
 5         :type k: int
 6         :rtype: List[List[int]]
 7         """
 8
 9         res = []
10         fin = []
11         self.helper(res, 0, n, k, fin)
12         return fin
13
14     def helper(self, res, s, n, k, fin):
15         if len(res) == k:
16             fin.append(list(res))   # python的深拷贝与浅拷贝
17             return
18         for i in range(s, n):
19             res.append(i+1)
20             self.helper(res, i+1, n, k, fin)
21             res.pop()

运行结果:  [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]

求子集的代码如下:
 1 #!/usr/bin/env python
 2 #coding=utf-8
 3
 4 def ziji(cur, n, a, b):
 5     if cur == n:
 6         b = []
 7         for i in range (0, n):
 8             if a[i] == 1:
 9                 b.append(i+1)
10         print b
11         return
12
13     a[cur] = 1
14     ziji(cur+1, n, a, b)
15     a[cur] = 0
16     ziji(cur+1, n, a, b)
17 a =  [0]*4
18 ziji(0, 4, a, []);
				
时间: 2024-10-05 06:33:56

leetcode 之 Combinations的相关文章

LeetCode: Letter Combinations of a Phone Number [018]

[题目] Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

Leetcode:Letter Combinations of a Phone Number 手机键盘字母映射

Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Outp

LeetCode:Combinations 题解

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], ]DFS,递归 1 class Solution { 2 public: 3 vector<vector<int> > an

LeetCode——Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

[LeetCode] Factor Combinations 因子组合

Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a function that takes an integer n and return all possible combinations of its factors. Note: Each combination's factors must be sorted ascending, for examp

[Lintcode]152. Combinations/[Leetcode]77. Combinations

152. Combinations/77. Combinations 本题难度: Medium Topic: Search & Recursion Description Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example Given n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2]

【leetcode】Combinations (middle)

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], ] 思路:有点像0-1背包问题, 对于从1-n的每一个数字都可以选择放入答案 和不放入答案. 当长度达到k时就是一个符合条件的解. 递归的

Java for LeetCode 077 Combinations

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], ] 解题思路: DFS,JAVA实现如下: public static void main(String args[]) { List<

【LeetCode】Combinations

Combinations 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], ] 递推点:加入i后,下一个加入的元素需要遍历i+1~n 因此可以基于k做递归. base case: k=1,

[LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q