File tree Expand file tree Collapse file tree 5 files changed +85
-0
lines changed
Expand file tree Collapse file tree 5 files changed +85
-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+ ans = []
4+ def combination (index , cur_comb , cur_sum ):
5+ if cur_sum == target :
6+ ans .append (cur_comb [:])
7+ return
8+ if cur_sum > target or index >= len (candidates ):
9+ return
10+ cur_comb .append (candidates [index ])
11+ combination (index , cur_comb , cur_sum + candidates [index ])
12+ # ํฉ์ด target์ด๋ ๊ฐ๋์ง, ํฌ๋์ง, ๊ธธ์ด๋ฅผ ๋๋์งํ๋ฉด return์ผ๋ก ํ์ถ
13+ # ๊ทธ ์ธ์ ๋ค๋ฅธ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ด์ผํ๋ฏ๋ก
14+ # ๋ง์ง๋ง๊บผ๋ ๋ค์ ๋นผ๊ณ ์ด์จ๋ index ๋๊ฒจ์ ๋ค์ combination ํ์ธํด๋ด์ผํจ
15+ cur_comb .pop ()
16+ combination (index + 1 , cur_comb , cur_sum )
17+ return ans
18+
19+ return combination (0 , [], 0 )
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def numDecodings (self , s : str ) -> int :
3+ memo = {}
4+
5+ def decode (index ):
6+ # ์ด๋ฏธ ๊ณ์ฐํ์ผ๋ฉด ๋ฐ๋ก ๋ฐํ
7+ if index in memo :
8+ return memo [index ]
9+
10+ # ๊ธฐ์ ์ฌ๋ก
11+ ## ๋๊น์ง ์์ผ๋ฉด ์ฑ๊ณต
12+ if index == len (s ):
13+ return 1
14+ ## 0์ผ๋ก ์์ํ๋ฉด ๋ถ๊ฐ๋ฅ
15+ if s [index ] == '0' :
16+ return 0
17+
18+ # ์ฌ๊ท ๊ณ์ฐ
19+ ways = decode (index + 1 )
20+
21+ if index + 1 < len (s ) and int (s [index :index + 2 ]) <= 26 :
22+ ways += decode (index + 2 )
23+
24+ # ๋ฉ๋ชจ์ด์ ์ด์
25+ memo [index ] = ways
26+ return ways
27+
28+ # ์๊ฐ๋ณต์ก๋, ๊ณต๊ฐ๋ณต์ก๋ O(n)
29+
30+ return decode (0 )
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def maxSubArray (self , nums : List [int ]) -> int :
3+ max_total = nums [0 ]
4+ total = 0
5+ # subarray๋ array์์ ์๋ก ์ธ์ ํด์ผํจ
6+ # ์ธ์ ํ ๊ฐ์ ํฉ์ด ๋ง์ด๋์ค์ธ ๊ฒฝ์ฐ, ๊ทธ๋ฅ ํ์ฌ ๊ฐ๋ง ์ฌ์ฉํ๋๊ฒ ํฉ๋ณด๋ค ํผ
7+ # total ์ด 0๋ณด๋ค ์์๊ฒฝ์ฐ ๊ทธ๋ฅ 0์ผ๋ก ๋ณ๊ฒฝ
8+ for num in nums :
9+ if total < 0 :
10+ total = 0
11+ total += num
12+ max_total = max (total , max_total )
13+ # ์๊ฐ๋ณต์ก๋ O(n), ๊ณต๊ฐ๋ณต์ก๋ O(1)
14+ return max_total
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def hammingWeight (self , n : int ) -> int :
3+ count = 0
4+ # ์๊ฐ๋ณต์ก๋ O(log n) ๋ฐ์ผ๋ก ๋๋๊ธฐ ๋๋ฌธ
5+ while n > 0 :
6+ if n % 2 == 1 :
7+ count += 1
8+ n = n // 2
9+ else :
10+ n = n / 2
11+ return count
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def isPalindrome (self , s : str ) -> bool :
3+ # ์๊ฐ๋ณต์ก๋ O(n)
4+ # loop
5+ s_list = [x .lower () for x in s if x .isalnum ()]
6+ # ์๊ฐ๋ณต์ก๋ O(n)
7+ # loop + list pop()
8+ for i in range (len (s_list )// 2 ):
9+ if s_list [i ] != s_list .pop ():
10+ return False
11+ return True
You canโt perform that action at this time.
0 commit comments