Skip to content

Commit 3645265

Browse files
Add files via upload
1 parent 9fdd210 commit 3645265

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Combination Sum/Combination_Sum.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 76ms 81.65%
2+
class Solution:
3+
def combinationSum(self, candidates, target):
4+
"""
5+
:type candidates: List[int]
6+
:type target: int
7+
:rtype: List[List[int]]
8+
"""
9+
# 把reslist写在前面会大幅度提高处理速度
10+
res_list, len_nums = [], len(candidates)
11+
candidates.sort()
12+
13+
def mysum(target, temp_list, start_index = 0):
14+
for index in range(start_index, len_nums):
15+
temp = target - candidates[index]
16+
if temp is 0:
17+
res_list.append(temp_list + [candidates[index]])
18+
elif temp < 0:
19+
break
20+
else:
21+
# 这里为了去重,增加了一个start_index
22+
mysum(temp, temp_list + [candidates[index]], index)
23+
24+
mysum(target, [])
25+
return res_list

0 commit comments

Comments
 (0)