-
Notifications
You must be signed in to change notification settings - Fork 0
/
77.py
46 lines (42 loc) · 1.2 KB
/
77.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
#77
class Solution(object):
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
ret = []
self.dfs(list(range(1, n+1)), k, [], ret)
return ret
def dfs(self, nums, k, path, ret):
print(nums,path,ret)
if len(path) == k:
ret.append(path)
return
for i in range(len(nums)):
print(i)
#print(nums[i+1:],k,path+[nums[i]],ret)
self.dfs(nums[i+1:], k, path+[nums[i]], ret)
# flow of the dfs
# ([1, 2, 3, 4], [], [])
# i=0
# ([2, 3, 4], [1], [])
# i=0 (inner loops)
# ([3, 4], [1, 2], [])
# i=1
# ([4], [1, 3], [[1, 2]])
# i=2
# ([], [1, 4], [[1, 2], [1, 3]])
# i=1
# ([3, 4], [2], [[1, 2], [1, 3], [1, 4]])
# i=0
# ([4], [2, 3], [[1, 2], [1, 3], [1, 4]])
# i=1
# ([], [2, 4], [[1, 2], [1, 3], [1, 4], [2, 3]])
# i=2
# ([4], [3], [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4]])
# i=0
# ([], [3, 4], [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4]])
# i=3
# ([], [4], [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]])