File tree Expand file tree Collapse file tree 5 files changed +80
-0
lines changed Expand file tree Collapse file tree 5 files changed +80
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def combinationSum (self , candidates : list [int ], target : int ) -> list [list [int ]]:
3
+ result = []
4
+
5
+ def backtrack (start : int , target : int , current : list [int ]):
6
+ if target == 0 :
7
+ result .append (current [:])
8
+ return
9
+
10
+ for i in range (start , len (candidates )):
11
+ if candidates [i ] > target :
12
+ continue
13
+
14
+ current .append (candidates [i ])
15
+ backtrack (i , target - candidates [i ], current )
16
+ current .pop ()
17
+
18
+ candidates .sort ()
19
+ backtrack (0 , target , [])
20
+ return result
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numDecodings (self , s : str ) -> int :
3
+ if not s or s [0 ] == '0' :
4
+ return 0
5
+
6
+ n = len (s )
7
+ dp = [0 ] * (n + 1 )
8
+ dp [0 ] = 1
9
+ dp [1 ] = 1
10
+
11
+ for i in range (2 , n + 1 ):
12
+ if s [i - 1 ] != '0' :
13
+ dp [i ] += dp [i - 1 ]
14
+
15
+ two_digit = int (s [i - 2 :i ])
16
+ if 10 <= two_digit <= 26 :
17
+ dp [i ] += dp [i - 2 ]
18
+
19
+ return dp [n ]
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def maxSubArray (self , nums : list [int ]) -> int :
3
+ max_sum = nums [0 ]
4
+ current_sum = nums [0 ]
5
+
6
+ for num in nums [1 :]:
7
+ current_sum = max (num , current_sum + num )
8
+ max_sum = max (max_sum , current_sum )
9
+
10
+ return max_sum
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def hammingWeight (self , n : int ) -> int :
3
+ count = 0
4
+ while n :
5
+ count += n & 1
6
+ n >>= 1
7
+ return count
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPalindrome (self , s : str ) -> bool :
3
+ filtered = '' .join (char .lower () for char in s if char .isalnum ())
4
+ end = len (filtered ) - 1
5
+ if end <= 0 :
6
+ return True
7
+ start = 0
8
+ while True :
9
+ end_s = filtered [end ]
10
+ start_s = filtered [start ]
11
+ if end_s == start_s :
12
+ end -= 1
13
+ start += 1
14
+
15
+
16
+ else :
17
+ return False
18
+ if start >= end :
19
+ return True
20
+ if end <= 0 :
21
+ return True
22
+ continue
23
+
24
+
You can’t perform that action at this time.
0 commit comments