File tree Expand file tree Collapse file tree 5 files changed +86
-0
lines changed Expand file tree Collapse file tree 5 files changed +86
-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
+
4
+ answer = []
5
+
6
+ # ์ฌ๊ท
7
+ def backtrack (start , path , total ):
8
+ # total์ด target๊ณผ ๊ฐ์์ง๋ฉด path๋ณต์ฌํด์ answer์ ์ถ๊ฐํ๊ณ ์ข
๋ฃ
9
+ if total == target :
10
+ answer .append (path [:])
11
+ return
12
+
13
+ # total์ด target๊ฐ ๋์ด๊ฐ๋ฉด ์ข
๋ฃ
14
+ if total > target :
15
+ return
16
+
17
+ for i in range (start , len (candidates )):
18
+ path .append (candidates [i ]) # ์ผ๋จ path์ ์ถ๊ฐํ๊ณ
19
+ backtrack (i , path , total + candidates [i ]) # ๊ฒ์ฆํ๊ธฐ
20
+ path .pop () # ๋ง์ง๋ง ๊ฐ ๊บผ๋ด๊ณ ๋ค์์ผ๋ก
21
+
22
+ # backtrack ํจ์ํธ์ถ
23
+ backtrack (0 , [], 0 )
24
+
25
+ return answer
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numDecodings (self , s : str ) -> int :
3
+
4
+ # DP
5
+ dp = [0 ]* (len (s )+ 1 )
6
+
7
+ # s๊ฐ 0์ผ๋ก ์์ํ๋ฉด 0 return
8
+ if s [0 ] == '0' :
9
+ return 0
10
+
11
+ dp [0 ] = 1 # ๋น๋ฌธ์์ด์ ํด์๊ฐ๋ฅํ 1๊ฐ์ง ๊ฒฝ์ฐ๋ก ์ทจ๊ธ (์ด๊ธฐ๊ธฐ์ค์ ์ญํ , dp[i-2]๊ณ์ฐ์ํ์)
12
+ dp [1 ] = 1 # ์ฒซ๋ฒ์งธ์๋ฆฌ์ ์ฒ๋ฆฌ๋ฐฉ๋ฒ์ 1๊ฐ์ง
13
+
14
+ # len(s)๊ฐ 2 ์ด์์ผ๋
15
+ for i in range (2 ,len (s )+ 1 ):
16
+ one = int (s [i - 1 ]) # ํ์๋ฆฌ(ํ์ฌ์๋ฆฌ)
17
+ two = int (s [i - 2 :i ]) # ํ์๋ฆฌ + ์์๋ฆฌ = ๋์๋ฆฌ
18
+
19
+ if 1 <= one <= 9 :
20
+ dp [i ] += dp [i - 1 ]
21
+ if 10 <= two <= 26 :
22
+ dp [i ] += dp [i - 2 ]
23
+
24
+ return dp [len (s )]
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def maxSubArray (self , nums : List [int ]) -> int :
3
+ # ์๊ฐ๋ณต์ก๋: O(n) - nums ๋ฐฐ์ด์ ํ ๋ฒ๋ง ์ํํจ
4
+ # ๊ณต๊ฐ๋ณต์ก๋: O(n) - dp ๋ฐฐ์ด์ nums ๊ธธ์ด๋งํผ ์์ฑ
5
+
6
+ # DP
7
+ dp = [0 ]* len (nums )
8
+ dp [0 ] = nums [0 ] # ์ด๊ธฐํ
9
+
10
+ for i in range (1 ,len (nums )):
11
+ # ํ์ฌ๊ฐ๊ณผ (์ด์ ๊น์ง์ ํฉ + ํ์ฌ๊ฐ) ์ค ๋ ํฐ ๊ฐ์ dp[i]์ ์ ์ฅ
12
+ dp [i ] = max (nums [i ], nums [i ]+ dp [i - 1 ])
13
+
14
+ return max (dp )
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def hammingWeight (self , n : int ) -> int :
3
+ answer = 0
4
+
5
+ while n > 0 :
6
+ answer += n % 2 #๋๋จธ์ง(ํ์ฌ ๋นํธ๊ฐ 1์ด๋ฉด ++)
7
+ n //= 2 #๋ชซ(๋ค์ ๋นํธ๋ก ์ด๋)
8
+
9
+ return answer
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPalindrome (self , s : str ) -> bool :
3
+
4
+ # ์๋ฌธ์๋ก ๋ณ๊ฒฝ
5
+ s = s .lower ()
6
+ p = ""
7
+
8
+ # ๋ฌธ์,์ซ์๋ง ๋ฝ๊ธฐ
9
+ for i in range (len (s )):
10
+ if (ord (s [i ]) > 96 and ord (s [i ]) < 123 ) or (ord (s [i ]) >= 48 and ord (s [i ]) <= 57 ):
11
+ p += s [i ]
12
+
13
+ # ๋ฌธ์์ด ๋ค์ง๊ธฐ
14
+ return p == p [::- 1 ]
You canโt perform that action at this time.
0 commit comments