46. Permutations
1 | def permutate(x): |
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2]]
46. Permutations
1 | def permutate(x): |
[[1, 2, 2], [2, 1, 2], [2, 2, 1]]
78. Subsets
Solution 1:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15def subset(x):
if len(x) == 0:
return []
if len(x) == 1:
return [x]
res = []
head = [x[0]]
tail = x[1:]
res.append(head)
for i in subset(tail):
res.append(i)
res.append(head + i)
return res
print(subset([1,2,3]))
Solution 2:1
2
3
4
5
6
7
8
9
10
11def backtracking( nums, temp, res, start):
res.append(list(temp))
#print(res)
for i in range(start,len(nums)):
temp.append(nums[i])
print(temp)
backtracking(nums, temp, res, i+1)
temp.pop()
return res
a=[1,2,3]
backtracking(a,[],[],0)
[1]
[1, 2]
[1, 2, 3]
[1, 3]
[2]
[2, 3]
[3]
[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
90. Subsets II
Solution 1:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16def subset(x):
if len(x) == 0:
return []
if len(x) == 1:
return [x]
res = []
head = [x[0]]
tail = x[1:]
res.append(head)
for i in subset(tail):
res.append(head + i)
if i not in res:
res.append(i)
return res
print(subset([1,1]))
Solution 2:1
2
3
4
5
6
7
8
9
10
11
12def backtracking( nums, temp, res, start):
if temp not in res:
res.append(list(temp))
#print(res)
for i in range(start,len(nums)):
temp.append(nums[i])
print(temp)
backtracking(nums, temp, res, i+1)
temp.pop()
return res
a=[1,2,2]
backtracking(sorted(a),[],[],0) # sorted is important
[1]
[1, 2]
[1, 2, 2]
[1, 2]
[2]
[2, 2]
[2]
[[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]
Solution 3:
1 | def backtracking(nums, temp, res, start): |
[[], [1], [1, 2], [1, 2, 1], [1, 1], [2], [2, 1], [1]]
39. Combination Sum
Solution 1:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25def combination_sum(x, target):
x = sorted(x)
def helper(temp,x,target):
if len(x) == 0 and len(temp) == 0:
return []
if len(x) == 0 and target != 0:
return []
if target ==0 and len(x) ==0 and len(temp) != 0:
return [temp]
res = []
first = x[0]
maxTime = target // first
for i in range(0, maxTime + 1):
newTarget = target - x[0] * i
tmp = helper(temp + [first] * i, x[1:], newTarget)
for j in tmp:
res.append(j)
return res
return helper([], x, target)
print(combination_sum([1,2,3,4,5],6))
Solution 2:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19def backtracking(x,target,temp,res,start):
if sum(temp)==target:
#print(type(temp))
#print(list(temp))
print(res)
res.append(list(temp)) # here we must use list(temp)
#print(res)
#print(res)
if sum(temp)>target:
return
if sum(temp)<target:
for i in range(start,len(x)):
temp.append(x[i])
#print(temp)
backtracking(x,target,temp,res,i)
temp.pop()
return res
a=[1,2,3]
backtracking(a,6,[],[],0)
[]
[[1, 1, 1, 1, 1, 1]]
[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2]]
[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2], [1, 1, 1, 3]]
[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2], [1, 1, 1, 3], [1, 1, 2, 2]]
[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2], [1, 1, 1, 3], [1, 1, 2, 2], [1, 2, 3]]
[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2], [1, 1, 1, 3], [1, 1, 2, 2], [1, 2, 3], [2, 2, 2]]
[[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 2],
[1, 1, 1, 3],
[1, 1, 2, 2],
[1, 2, 3],
[2, 2, 2],
[3, 3]]
1 | def f(a,b): |
[[3, 4, 4]]
[[3, 4]]
40. Combination Sum II
Solution 1: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
28def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
newCandidate = []
for i in candidates :
if i <= target:
newCandidate.append(i)
def helper(forbidden,temp,x,target):
if target < 0:
return []
if len(x) == 0 and len(temp) == 0:
return []
if len(x) == 0 and target != 0:
return []
if target ==0 and len(x) ==0 and len(temp) != 0:
return [temp]
first = x[0]
if first in forbidden:
return helper(forbidden, temp, x[1:], target)
else:
tmp1 = helper(forbidden + [first], temp, x[1:], target)
tmp2 = helper(forbidden, temp + [first], x[1:], target - first)
return tmp1 + tmp2
return helper([],[], newCandidate, target)
Solution 2:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19def backtracking(x,target,temp,res,start):
if sum(temp)==target:
#print(type(temp))
#print(list(temp))
print(res)
res.append(list(temp)) # here we must use list(temp)
#print(res)
#print(res)
if sum(temp)>target:
return
if sum(temp)<target:
for i in range(start,len(x)):
temp.append(x[i])
#print(temp)
backtracking(x,target,temp,res,i+1)
temp.pop()
return res
a=[1,2,3]
backtracking(a,6,[],[],0)
[]
[[1, 2, 3]]
216. Combination Sum III
1 | def backtracking(x,target,temp,res,start,k): |
[[1, 2, 4]]
22. Generate Parentheses
1 | def backtracking(x,temp,tempCount,res,n): |
['(']
['(', '(']
['(', '(', '(']
['(', '(', '(', '(']
['(', '(', '(', ')']
['(', '(', '(', ')', '(']
['(', '(', '(', ')', ')']
['(', '(', '(', ')', ')', '(']
['(', '(', '(', ')', ')', ')']
['(', '(', ')']
['(', '(', ')', '(']
['(', '(', ')', '(', '(']
['(', '(', ')', '(', ')']
['(', '(', ')', '(', ')', '(']
['(', '(', ')', '(', ')', ')']
['(', '(', ')', ')']
['(', '(', ')', ')', '(']
['(', '(', ')', ')', '(', '(']
['(', '(', ')', ')', '(', ')']
['(', '(', ')', ')', ')']
['(', ')']
['(', ')', '(']
['(', ')', '(', '(']
['(', ')', '(', '(', '(']
['(', ')', '(', '(', ')']
['(', ')', '(', '(', ')', '(']
['(', ')', '(', '(', ')', ')']
['(', ')', '(', ')']
['(', ')', '(', ')', '(']
['(', ')', '(', ')', '(', '(']
['(', ')', '(', ')', '(', ')']
['(', ')', '(', ')', ')']
['(', ')', ')']
[')']
['((()))', '(()())', '(())()', '()(())', '()()()']
320. Generalized Abbreviation
1 | def backtracking(x,temp,res,n): |
['4',
'3d',
'2r1',
'2rd',
'1o2',
'1o1d',
'1or1',
'1ord',
'w3',
'w2d',
'w1r1',
'w1rd',
'wo2',
'wo1d',
'wor1',
'word']
51. N queens
1 | def n_queen(n): |