Skip to content

Commit e63def2

Browse files
Chris WuChris Wu
Chris Wu
authored and
Chris Wu
committed
no message
1 parent 3735871 commit e63def2

6 files changed

+122
-0
lines changed

problems/combination-sum-ii.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,21 @@ def helper(combination, start, target_remain):
3636
candidates.sort()
3737
helper([], 0, target)
3838
return [list(combination) for combination in answer]
39+
40+
#DFS
41+
class Solution(object):
42+
def combinationSum2(self, candidates, T):
43+
def dfs(index, target, path):
44+
if target<0:
45+
return
46+
elif target==0:
47+
opt.append(path)
48+
else:
49+
for i in xrange(index, len(candidates)):
50+
if i>index and candidates[i]==candidates[i-1]: continue
51+
num = candidates[i]
52+
dfs(i+1, target-num, path+[num])
53+
opt = []
54+
candidates.sort()
55+
dfs(0, T, [])
56+
return opt

problems/combination-sum-iii.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,16 @@ def combinationSum3(self, K, N):
2525
ans.append(first)
2626
total+=first
2727
first+=1
28+
29+
#DFS
30+
class Solution(object):
31+
def combinationSum3(self, K, N):
32+
def dfs(path, min_num):
33+
if len(path)==K and sum(path)==N:
34+
opt.append(path)
35+
for num in xrange(min_num, 10):
36+
dfs(path+[num], num+1)
37+
opt = []
38+
dfs([], 1)
39+
return opt
40+

problems/combination-sum.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,47 @@ def helper(candidates, target, combination):
7474
else:
7575
return helper(candidates, target-n, combination+[n]) + helper(candidates[1:], target, combination)
7676
return helper(sorted(candidates), target, [])
77+
78+
79+
# 2019/9/12 Update
80+
class Solution(object):
81+
def combinationSum(self, candidates, T):
82+
answer = []
83+
ans = []
84+
first = 0
85+
total = 0
86+
87+
candidates.sort()
88+
89+
memo = {}
90+
for i, num in enumerate(candidates):
91+
memo[num] = i
92+
93+
while True:
94+
if total==T:
95+
answer.append(ans[:])
96+
if total>=T or first>=len(candidates):
97+
if not ans: return answer
98+
num = ans.pop()
99+
first = memo[num]+1
100+
total-=num
101+
else:
102+
ans.append(candidates[first])
103+
total+=candidates[first]
104+
105+
# DFS
106+
class Solution(object):
107+
def combinationSum(self, candidates, T):
108+
def dfs(index, target, path):
109+
if target<0:
110+
return
111+
elif target==0:
112+
opt.append(path)
113+
else:
114+
for i in xrange(index, len(candidates)):
115+
num = candidates[i]
116+
dfs(i, target-num, path+[num])
117+
opt = []
118+
candidates.sort()
119+
dfs(0, T, [])
120+
return opt

problems/combinations.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ def combine(self, N, K):
6666

6767

6868

69+
#DFS
70+
class Solution(object):
71+
def combine(self, N, K):
72+
def dfs(n_min, path):
73+
if len(path)==K:
74+
opt.append(path)
75+
return
76+
else:
77+
for n in xrange(n_min, N+1):
78+
dfs(n+1, path+[n])
79+
opt = []
80+
dfs(1, [])
81+
return opt
82+
83+
84+
6985

7086

7187

problems/subsets-ii.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,21 @@ def subsetsWithDup(self, nums):
5151
for s in power_set:
5252
answer.append(answer[i]+s)
5353
return answer
54+
55+
56+
#DFS
57+
class Solution(object):
58+
def subsetsWithDup(self, nums):
59+
nums.sort()
60+
61+
def dfs(path, nums):
62+
opt.append(path)
63+
if len(nums)==0: return
64+
for i, num in enumerate(nums):
65+
if i>0 and nums[i]==nums[i-1]: continue
66+
dfs(path+[num], nums[i+1:])
67+
opt = []
68+
dfs([], nums)
69+
return opt
70+
71+

problems/subsets.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,16 @@ def subsets(self, nums):
7272
The time complexity is O(2^N). The space complexity is O(2^N), too.
7373
(This solution is in spired by @ZitaoWang's elegant solution)
7474
"""
75+
76+
#DFS
77+
class Solution(object):
78+
def subsets(self, nums):
79+
def dfs(path, nums):
80+
opt.append(path)
81+
if len(nums)==0: return
82+
for i, num in enumerate(nums):
83+
dfs(path+[num], nums[i+1:])
84+
85+
opt = []
86+
dfs([], nums)
87+
return opt

0 commit comments

Comments
 (0)