forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinations.py
More file actions
34 lines (32 loc) · 1.02 KB
/
combinations.py
File metadata and controls
34 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
class Solution:
"""
@param n: Given the range of numbers
@param k: Given the numbers of combinations
@return: All the combinations of k numbers out of 1..n
"""
def combine(self, n, k):
# write your code here
# TODO: 添加注释
list = [x + 1 for x in xrange(n)]
return self.dfs(list, 0, k)
def dfs(self, list, pos, k):
n = len(list) - pos
if (not list) or (n < k):
return None
ret = []
if k == 0:
return [[]]
elif k == 1:
for i in xrange(pos, len(list), 1):
ret.append([list[i]])
return ret
else:
for i in xrange(pos, len(list), 1):
combs = self.dfs(list, i + 1, k - 1)
if combs:
for comb in combs:
new_comb = [list[i]]
new_comb.extend(comb)
ret.append(new_comb)
return ret