diff --git a/combination-sum/printjin-gmailcom.py b/combination-sum/printjin-gmailcom.py new file mode 100644 index 000000000..94afe2604 --- /dev/null +++ b/combination-sum/printjin-gmailcom.py @@ -0,0 +1,15 @@ +class Solution: + def combinationSum(self, candidates, target): + output, nums = [], [] + def dfs(start, total): + if total > target: + return + if total == target: + return output.append(nums[:]) + for i in range(start, len(candidates)): + num = candidates[i] + nums.append(num) + dfs(i, total + num) + nums.pop() + dfs(0, 0) + return output diff --git a/decode-ways/printjin-gmailcom.py b/decode-ways/printjin-gmailcom.py new file mode 100644 index 000000000..ffec156e3 --- /dev/null +++ b/decode-ways/printjin-gmailcom.py @@ -0,0 +1,13 @@ +class Solution: + def numDecodings(self, s): + if not s: + return 0 + dp = [0] * (len(s) + 1) + dp[0] = 1 + dp[1] = 1 if s[0] != '0' else 0 + for i in range(2, len(s) + 1): + if '1' <= s[i - 1] <= '9': + dp[i] += dp[i - 1] + if '10' <= s[i - 2:i] <= '26': + dp[i] += dp[i - 2] + return dp[len(s)] diff --git a/maximum-subarray/printjin-gmailcom.py b/maximum-subarray/printjin-gmailcom.py new file mode 100644 index 000000000..4e420ab67 --- /dev/null +++ b/maximum-subarray/printjin-gmailcom.py @@ -0,0 +1,7 @@ +class Solution: + def maxSubArray(self, nums): + max_sum = current_sum = nums[0] + for num in nums[1:]: + current_sum = max(num, current_sum + num) + max_sum = max(max_sum, current_sum) + return max_sum diff --git a/number-of-1-bits/printjin-gmailcom.py b/number-of-1-bits/printjin-gmailcom.py new file mode 100644 index 000000000..13a6eb9ba --- /dev/null +++ b/number-of-1-bits/printjin-gmailcom.py @@ -0,0 +1,7 @@ +class Solution: + def hammingWeight(self, n): + cnt = 0 + while n > 0: + cnt += n % 2 + n //= 2 + return cnt diff --git a/valid-palindrome/printjin-gmailcom.py b/valid-palindrome/printjin-gmailcom.py new file mode 100644 index 000000000..a58734637 --- /dev/null +++ b/valid-palindrome/printjin-gmailcom.py @@ -0,0 +1,4 @@ +class Solution: + def isPalindrome(self, s): + cleaned = [c.lower() for c in s if c.isalnum()] + return cleaned == cleaned[::-1]