File tree Expand file tree Collapse file tree 4 files changed +105
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 4 files changed +105
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
3+ nums .sort () # ๋ฐฐ์ด ์ ๋ ฌํด์ ์ค๋ณต ์ฒ๋ฆฌ์ ํฌ ํฌ์ธํฐ์ ์ ๋ฆฌํ๊ฒ ๋ง๋ฆ
4+ n = len (nums )
5+ answer = []
6+
7+ for i in range (n - 2 ):
8+ # i๊ฐ 0์ด ์๋๋ฉด์ ์ด์ ๊ฐ๊ณผ ๊ฐ์ผ๋ฉด ์ค๋ณต ๋ฐฉ์ง๋ฅผ ์ํด ๊ฑด๋๋
9+ if i > 0 and nums [i ] == nums [i - 1 ]:
10+ continue
11+
12+ left , right = i + 1 , n - 1
13+
14+ while left < right :
15+ total = nums [i ] + nums [left ] + nums [right ]
16+
17+ if total == 0 :
18+ answer .append ([nums [i ], nums [left ], nums [right ]])
19+
20+ # left์ right๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ด ์ค๋ณต์ด๋ฉด ๋์ด๊ฐ
21+ while left < right and nums [left ] == nums [left + 1 ]:
22+ left += 1
23+ while left < right and nums [right ] == nums [right - 1 ]:
24+ right -= 1
25+
26+ left += 1
27+ right -= 1
28+ elif total < 0 :
29+ left += 1
30+ else :
31+ right -= 1
32+
33+ return answer
Original file line number Diff line number Diff line change 1+ class Solution {
2+ // ์ ์๊น์ง n๊ฑธ์์ด ์๋ชจ๋๋ค๊ณ ํ ๋, n์ ๊ตฌํ ์ ์๋ ์ค๋ณต๋์ง ์๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ณ์ฐํ๋ ๋ฌธ์
3+ // ๊ณ๋จ์ 1๊ฑธ์์ด๋ 2๊ฑธ์์ฉ ์ฌ๋ผ๊ฐ ์ ์๋ค.
4+ fun climbStairs (n : Int ): Int {
5+ // ๊ณ๋จ์ด 1๊ฐ๋ผ๋ฉด ๋ฌด์กฐ๊ฑด 1์ ๋ฐํ
6+ if (n == 1 ) return 1
7+ // ์ซ์๋ก ๋ ๋ฐฐ์ด์ ์ ์ธ(ํธ์์ 1๋ฒ์งธ ๊ณ๋จ๋ถํฐ ์์ํ๋๋ก n+1๋ก ์ ์ธ)
8+ val dp = IntArray (n + 1 )
9+ // ์ฒซ๋ฒ์งธ ๊ณ๋จ์ ์ฌ๋ผ๊ฐ๋ ๋ฐฉ๋ฒ์ 1๊ฐ์ง
10+ dp[1 ] = 1
11+ // ๋๋ฒ์งธ ๊ณ๋จ๊น์ง ์ฌ๋ผ๊ฐ๋ ๋ฐฉ๋ฒ์ 2๊ฐ์ง(1+1, 2)
12+ dp[2 ] = 2
13+
14+ // ์ด์ 3๋ฒ์งธ ๊ณ๋จ๋ถํฐ n๋ฒ์งธ ๊ณ๋จ์ธ ์ ์๊น์ง ์ฌ๋ผ๊ฐ๋ ๋ฐฉ๋ฒ์ ๊ณ์ฐ
15+ for (i in 3 .. n){
16+ dp[i] = dp[i - 1 ] + dp[i - 2 ]
17+ }
18+ return dp[n]
19+ }
20+ }
21+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ # ์ ์ ๋ฐฐ์ด์ ๋ํด answer ๋ฐฐ์ด์ ๋ฐํ
3+ # 1. ์ ๋ต ๋ฐฐ์ด์ i๋ฒ์งธ ์์๋ ์ ์ ๋ฐฐ์ด์ i๋ฒ์งธ ์์๋ฅผ ์ ์ธํ ๋ค๋ฅธ ๋ชจ๋ ์์์ ๊ณฑ
4+ # 2. ์๊ฐ๋ณต์ก๋๋ O(n) ์ด๋ด๋ก ํด๊ฒฐ ํ์
5+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
6+ length = len (nums )
7+ answer = [1 ] * length # 1๋ก ์ด๊ธฐํ
8+
9+ # ์ผ์ชฝ ๊ณฑ ๊ณ์ฐ ํ answer์ ์ ์ฅ
10+ left = 1
11+ for i in range (length ):
12+ answer [i ] = left
13+ left *= nums [i ]
14+
15+ # ์ค๋ฅธ์ชฝ ๊ณฑ์ ๋์ ํ๋ฉด์ answer์ ๊ณฑํ๊ธฐ
16+ right = 1
17+ for i in range (length - 1 , - 1 , - 1 ):
18+ answer [i ] *= right
19+ right *= nums [i ]
20+
21+ return answer
22+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ // t๊ฐ s์ ์ฒ ์๋ฅผ ๋ชจ๋ ํฌํจํ๊ณ ์๋ ์ ๋๊ทธ๋จ์ธ์ง๋ฅผ ํ์ธํ๋ ๋ฌธ์
3+ // ํธ๋ ๋ฐฉ๋ฒ์ 2๊ฐ์ง
4+ // 1. ์ ๋์ฝ๋๋ก ๋ณํํด์ sort ํด์ ํธ๋ ๋ฐฉ๋ฒ
5+ // 2. ์ ๋ ฌํ์ง ์๊ณ mutableMap์ผ๋ก ๋งต์ ๋ง๋ค์ด ํธ๋ ๋ฐฉ๋ฒ
6+ // 2๋ฒ์ผ๋ก ๋ฌธ์ ๋ฅผ ํ์๋ค.
7+ fun isAnagram (s : String , t : String ): Boolean {
8+ // ์ฐ์ ์ฒ์๋ถํฐ ๋ String์ ๊ธธ์ด๊ฐ ๋ค๋ฅด๋ฉด ๋ฐ๋ก false๋ฅผ ๋ฐํ
9+ if (s.length != t.length) false
10+
11+ // s์ ๋ฌธ์์ด์ ์นด์ดํธํ๋ฉด์ ์ ์ฅํ ๊ฐ๋ณ๋ฐฐ์ด์ ์ ์ธํด์ค๋ค.
12+ val countMap = mutableMapOf<Char , Int >()
13+
14+ // s์ ์๋ ๊ฐ๊ฐ์ ๋ฌธ์๋ค์ ๋ํด์ ๋ฐ๋ณต๋ฌธ์ ๋๋ฉด์ ์ถํํ์๋ฅผ ๋งต์ผ๋ก ๊ตฌ์ฑ
15+ for (c in s){
16+ countMap[c] = countMap.getOrDefault(c, 0 ) + 1
17+ }
18+
19+ // ์ด์ t์ ๋ฌธ์๋ค์ด ์๊น ๋ง๋ค์ด์ง countMap์ ๋ชจ๋ ์กด์ฌํ๋์ง ํ์ธ
20+ for (c in t){
21+ val count = countMap.getOrDefault(c, 0 )
22+ if (count == 0 ) return false
23+ countMap[c] = count - 1
24+ }
25+ // countMap์ ๋ค์ด์๋ value๊ฐ ์ ๋ถ 0์ด๋ฉด true๋ฅผ ๋ฐํ
26+ return countMap.values.all { it == 0 }
27+ }
28+ }
29+
You canโt perform that action at this time.
0 commit comments