-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbacktrack_template.py
69 lines (52 loc) · 1.38 KB
/
backtrack_template.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 无duplicates(distinct or unique 不可复选
# combination/subset
def backtrack(nums, start):
for i in range(start, len(nums)):
# 做选择
track.append(nums[i])
# 注意参数
backtrack(nums, i+1)
# 撤销选择
track.pop(-1)
# permutation
def backtrack(nums, used):
for i in range(len(nums)):
# 剪枝逻辑
if used[i] == True:
continue
# 做选择
track.append(nums[i])
used[i] = True
backtrack(nums, used)
# 撤销选择
track.pop(-1)
used[i] = False
# nums元素有重复不可复选
# combination/subset
nums = sorted(nums)
def backtrack(start, nums):
for i in range(start, nums):
if nums[i-1] == nums[i] and i > start:
continue
track.append(nums[i])
backtrack(start+1, nums)
track.pop(-1)
# permutation
nums = sorted(nums)
def backtrack(used, nums):
for i in range(0, len(nums)):
if used[i] == True:
continue
if i > 0 and nums[i] == nums[i-1] and used[i-1] == False:
continue
track.append(nums[i])
used[i] = True
backtrack(nums)
track.pop(-1)
used[i] = False
# 无重可复选
def backtrack(nums, start):
for i in range(0, len(nums)):
track.append(i)
backtrack(nums, i)
track.pop(-1)