-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution77_combines.py
59 lines (40 loc) · 1.09 KB
/
Solution77_combines.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 我的思路 loop 不出来 n=4,k=2 好理解,但是n=4, k=3就不好求了
def combine(n, k):
value_range = []
res = []
i = 1
while i <= n:
value_range.append(i)
i += 1
for left in range(len(value_range)):
right = left + (k - 1)
while right <= len(value_range) - 1:
temp = []
temp = temp.append(value_range[left])
temp = temp.append(value_range[right])
res.append(temp)
return res
# backtracking 结构
def combine2(n, k):
def backtrack(first=1, curr=[]):
#print(str(curr))
# if the combination is done
if len(curr) == k:
output.append(curr[:])
return # 这里应该加一个return
for i in range(first, n + 1):
# add i into the current combination
curr.append(i)
# use next integers to complete the combination
backtrack(i + 1, curr)
# backtrack
curr.pop()
output = []
backtrack()
return output
n = 4
k = 2
r = combine2(4, 2)
print(r)
"""
"""