Skip to content

Commit dc865f9

Browse files
authored
[chordpli] Week3 Solutions (#1808)
1 parent cef899b commit dc865f9

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

combination-sum/chordpli.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
6+
result = []
7+
candidates.sort()
8+
self.__backtrack__(candidates, 0, target, result, [])
9+
return result
10+
11+
def __backtrack__(self, candidates: List[int], current_idx: int, target: int, result: List[List[int]],
12+
current_arr: List[int]):
13+
if current_idx >= len(candidates):
14+
return
15+
16+
add_value = sum(current_arr) + candidates[current_idx]
17+
18+
if add_value == target:
19+
current_arr.append(candidates[current_idx])
20+
result.append(current_arr.copy())
21+
current_arr.pop()
22+
23+
if add_value > target:
24+
return self.__backtrack__(candidates, current_idx + 1, target, result, current_arr)
25+
26+
current_arr.append(candidates[current_idx])
27+
self.__backtrack__(candidates, current_idx, target, result, current_arr)
28+
current_arr.pop()
29+
self.__backtrack__(candidates, current_idx + 1, target, result, current_arr)

number-of-1-bits/chordpli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
return bin(n).count('1')

valid-palindrome/chordpli.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import re
2+
3+
4+
class Solution:
5+
def isPalindrome(self, s: str) -> bool:
6+
lower_s = s.lower()
7+
lower_eng_str = re.sub(r"[^a-z0-9]", "", lower_s)
8+
reverse_str = lower_eng_str[::-1]
9+
10+
if lower_eng_str == reverse_str:
11+
return True
12+
13+
return False

0 commit comments

Comments
 (0)