File tree Expand file tree Collapse file tree 5 files changed +70
-0
lines changed
Expand file tree Collapse file tree 5 files changed +70
-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+ dp = {}
4+
5+ for i in range (target + 1 ):
6+ dp [i ] = set ()
7+
8+ for candidate in candidates :
9+ if candidate <= target :
10+ dp [candidate ].add (tuple ([candidate ]))
11+
12+ for i in range (target + 1 ):
13+ for candidate in candidates :
14+ if i - candidate >= 0 :
15+ for j in dp [i - candidate ]:
16+ arr = [nums for nums in j ]+ [candidate ]
17+ arr .sort ()
18+ dp [i ].add (tuple (arr ))
19+
20+ answer = [list (nums ) for nums in dp [target ]]
21+ return answer
22+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def numDecodings (self , s : str ) -> int :
3+ dp = [0 ]* (len (s )+ 1 )
4+ s = '0' + s
5+ dp [0 ] = 1
6+ for i in range (1 , len (s )):
7+ if int (s [i ]) == 0 :
8+ if int (s [i - 1 ]) != 1 and int (s [i - 1 ]) != 2 :
9+ return 0
10+ else :
11+ dp [i ]= dp [i - 2 ]
12+ elif int (s [i ]) <= 6 :
13+ if int (s [i - 1 ]) != 1 and int (s [i - 1 ]) != 2 :
14+ dp [i ]= dp [i - 1 ]
15+ else :
16+ dp [i ]= dp [i - 1 ]+ dp [i - 2 ]
17+ else :
18+ if int (s [i - 1 ]) == 1 :
19+ dp [i ]= dp [i - 1 ]+ dp [i - 2 ]
20+ else :
21+ dp [i ]= dp [i - 1 ]
22+ return dp [- 1 ]
23+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def maxSubArray (self , nums : List [int ]) -> int :
3+ s = 0
4+ min_s = 0
5+ max_s = nums [0 ]
6+
7+ for num in nums :
8+ s += num
9+ if max_s < s - min_s :
10+ max_s = s - min_s
11+ if min_s > s :
12+ min_s = s
13+ return max_s
14+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ hammingWeight = lambda _ , n : n .bit_count ()
3+
4+
5+
Original file line number Diff line number Diff line change 1+ import re
2+ class Solution :
3+ def isPalindrome (self , s : str ) -> bool :
4+ parsed_string = re .sub ('[^a-zA-Z0-9]' ,'' ,s ).upper ()
5+ return parsed_string == parsed_string [::- 1 ]
6+
You can’t perform that action at this time.
0 commit comments