diff --git a/combination-sum/doh6077.py b/combination-sum/doh6077.py new file mode 100644 index 0000000000..e769f950df --- /dev/null +++ b/combination-sum/doh6077.py @@ -0,0 +1,20 @@ +class Solution: + + def solve(self, candidates, remsum, cur, res, idx) : + if remsum == 0 : + res.append(list(cur)) + return + + if remsum < 0 or idx >= len(candidates) : + return + cur.append(candidates[idx]) + self.solve(candidates, remsum-candidates[idx],cur,res,idx) + cur.pop() + self.solve(candidates,remsum,cur,res,idx+1) + + + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + cur = [] + res = [] + self.solve(candidates,target,cur,res,0) + return res diff --git a/decode-ways/doh6077.py b/decode-ways/doh6077.py new file mode 100644 index 0000000000..2d440dc01c --- /dev/null +++ b/decode-ways/doh6077.py @@ -0,0 +1,20 @@ +class Solution: + def numDecodings(self, s: str) -> int: + if s[0] == '0': + return 0 + n = len(s) + cur = 1 + pre1 = pre2 = 1 + for i in range(2, n+1): + one = int(s[i-1]) + two = int(s[i-2:i]) + cur = 0 + if 1 <= one <= 9: + cur += pre1 + if 10 <= two <= 26: + cur += pre2 + + pre2 = pre1 + pre1 = cur + + return cur diff --git a/maximum-subarray/doh6077.py b/maximum-subarray/doh6077.py new file mode 100644 index 0000000000..f6dba8138f --- /dev/null +++ b/maximum-subarray/doh6077.py @@ -0,0 +1,13 @@ +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + res = nums[0] + total = 0 + + for n in nums: + if total < 0: + total = 0 + + total += n + res = max(res, total) + + return res diff --git a/number-of-1-bits/doh6077.py b/number-of-1-bits/doh6077.py new file mode 100644 index 0000000000..b04b5900c5 --- /dev/null +++ b/number-of-1-bits/doh6077.py @@ -0,0 +1,7 @@ +class Solution: + def hammingWeight(self, n: int) -> int: + ans = 0 + while n != 0: + ans += 1 + n = n & ( n-1) + return ans diff --git a/valid-palindrome/doh6077.py b/valid-palindrome/doh6077.py new file mode 100644 index 0000000000..cb2b3e5600 --- /dev/null +++ b/valid-palindrome/doh6077.py @@ -0,0 +1,16 @@ +import re +# two pointers +class Solution: + def isPalindrome(self, s: str) -> bool: + # remove all alphanumeric characters and convert all uppercase letters into lowercase letters + cleaned_s = re.sub(r'[^a-zA-Z0-9]', '', s).lower() + n = len(cleaned_s) + left, right = 0, n -1 + while left < right: + if cleaned_s[left] == cleaned_s[right]: + left += 1 + right -= 1 + else: + return False + + return True