Skip to content

Commit 7958e43

Browse files
Merge pull request #1275 from printjin-gmailcom/main
[printjin-gmailcom] WEEK 03 solutions
2 parents cd5f9f7 + ae107c8 commit 7958e43

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

combination-sum/printjin-gmailcom.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def combinationSum(self, candidates, target):
3+
output, nums = [], []
4+
def dfs(start, total):
5+
if total > target:
6+
return
7+
if total == target:
8+
return output.append(nums[:])
9+
for i in range(start, len(candidates)):
10+
num = candidates[i]
11+
nums.append(num)
12+
dfs(i, total + num)
13+
nums.pop()
14+
dfs(0, 0)
15+
return output

decode-ways/printjin-gmailcom.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def numDecodings(self, s):
3+
if not s:
4+
return 0
5+
dp = [0] * (len(s) + 1)
6+
dp[0] = 1
7+
dp[1] = 1 if s[0] != '0' else 0
8+
for i in range(2, len(s) + 1):
9+
if '1' <= s[i - 1] <= '9':
10+
dp[i] += dp[i - 1]
11+
if '10' <= s[i - 2:i] <= '26':
12+
dp[i] += dp[i - 2]
13+
return dp[len(s)]

maximum-subarray/printjin-gmailcom.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def maxSubArray(self, nums):
3+
max_sum = current_sum = nums[0]
4+
for num in nums[1:]:
5+
current_sum = max(num, current_sum + num)
6+
max_sum = max(max_sum, current_sum)
7+
return max_sum

number-of-1-bits/printjin-gmailcom.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def hammingWeight(self, n):
3+
cnt = 0
4+
while n > 0:
5+
cnt += n % 2
6+
n //= 2
7+
return cnt

valid-palindrome/printjin-gmailcom.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def isPalindrome(self, s):
3+
cleaned = [c.lower() for c in s if c.isalnum()]
4+
return cleaned == cleaned[::-1]

0 commit comments

Comments
 (0)