Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 42921fa

Browse files
python: add problem 39 and unittest
1 parent e16c45e commit 42921fa

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Python/sln_1_100/solution_31_40.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: UTF-8 -*-
2+
import copy
23
from typing import List
34

45

@@ -80,3 +81,20 @@ def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
8081
:param target:
8182
:return:
8283
"""
84+
candidates = sorted(candidates)
85+
results = []
86+
self.recursive_search(candidates, target, target, 0, [], results)
87+
return results
88+
89+
def recursive_search(self, candidiates, remain_target, target, start_idx, current_list, results):
90+
if remain_target == 0:
91+
results.append(copy.deepcopy(current_list))
92+
else:
93+
for idx in range(start_idx, len(candidiates)):
94+
if remain_target - candidiates[idx] >= 0:
95+
current_list.append(candidiates[idx])
96+
self.recursive_search(candidiates, remain_target - candidiates[idx], target,
97+
idx, current_list, results)
98+
current_list.pop()
99+
else:
100+
break

Python/sln_1_100/test_solution_31_40.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ def test_searchInsert(self):
3535
def test_countAndSay(self):
3636
self.assertEqual(self.sln.countAndSay(2), '11')
3737
self.assertEqual(self.sln.countAndSay(4), '1211')
38+
39+
def test_combinationSum(self):
40+
ret = self.sln.combinationSum([2, 3, 6, 7], 7)
41+
print(ret)

0 commit comments

Comments
 (0)