File tree Expand file tree Collapse file tree 3 files changed +68
-0
lines changed
Expand file tree Collapse file tree 3 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ λ¬Έμ μμ½
3+ - candidates λ°°μ΄μμ μ«μλ₯Ό 무μ ν μ¬μ©νμ¬ ν©μ΄ targetμ΄ λλ λͺ¨λ μ‘°ν© μ°ΎκΈ°
4+
5+ μμ΄λμ΄
6+ - λ°±νΈλνΉ: iλ²μ§Έ μΈλ±μ€λΆν° νμνμ¬ μ€λ³΅ μ‘°ν© λ°©μ§
7+ - κ°μ μ«μ μ¬μ¬μ© κ°λ₯ β μ¬κ· μ μΈλ±μ€ i μ μ§
8+ - μ λ ¬ ν target μ΄κ³Ό μ breakλ‘ κ°μ§μΉκΈ°
9+
10+ μκ°λ³΅μ‘λ: O(N^(T/M)) - N: candidates κΈΈμ΄, T: target, M: μ΅μκ°
11+ 곡κ°λ³΅μ‘λ: O(T/M) - μ¬κ· κΉμ΄
12+ """
13+
14+ class Solution :
15+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
16+ result = []
17+ sol = []
18+ candidates .sort ()
19+ n = len (candidates )
20+
21+ def backtrack (start , cur_sum ):
22+ if cur_sum == target :
23+ result .append (sol .copy ())
24+ return
25+
26+ for i in range (start , n ):
27+ if cur_sum + candidates [i ] > target :
28+ break
29+ sol .append (candidates [i ])
30+ backtrack (i , cur_sum + candidates [i ])
31+ sol .pop ()
32+
33+ backtrack (0 , 0 )
34+ return result
Original file line number Diff line number Diff line change 1+ """
2+ λ¬Έμ μμ½
3+ - 32λΉνΈ λΆνΈ μλ μ μ nμ μ΄μ§ ννμμ '1'μ κ°μ(= set bits)λ₯Ό ꡬνλΌ.
4+
5+ μμ΄λμ΄ A: λΈλΌμ΄μΈ 컀λνΈ(Brian Kernighan) μκ³ λ¦¬μ¦
6+ - ν΅μ¬ κ΄μ°°: n & (n - 1)μ nμ μ΅νμ 1λΉνΈλ₯Ό '0'μΌλ‘ λ§λ λ€.
7+ μ) n = 0b101100 β n-1 = 0b101011 β n & (n-1) = 0b101000 (μ΅νμ 1 νλ μμ )
8+ - λ°λΌμ '1'μ κ°μλ§νΌ 루νκ° λλ€.
9+ - μκ°λ³΅μ‘λ: O(k) (k = nμ 1λΉνΈ κ°μ) β λ§€μ° λΉ λ¦
10+ - 곡κ°λ³΅μ‘λ: O(1)
11+ """
12+
13+ class Solution :
14+ def hammingWeight (self , n : int ) -> int :
15+ cnt = 0
16+ while n :
17+ n &= n - 1 # μ΅νμ 1λΉνΈ μ κ±°
18+ cnt += 1
19+ return cnt
Original file line number Diff line number Diff line change 1+ """
2+ λ¬Έμ μμ½
3+ - μμ«μλ§ κ³ λ €, λμλ¬Έμ 무μ β ν°λ¦°λ둬 μ¬λΆ
4+
5+ μμ΄λμ΄
6+ - μλ¬Έμ λ³ν β μμ«μλ§ νν° β μμκ³Ό λμΌνμ§ λΉκ΅
7+
8+ μκ°λ³΅μ‘λ: O(n)
9+ 곡κ°λ³΅μ‘λ: O(n)
10+ """
11+
12+ class Solution :
13+ def isPalindrome (self , s : str ) -> bool :
14+ cleaned = [c for c in s .lower () if c .isalnum ()]
15+ return cleaned == cleaned [::- 1 ]
You canβt perform that action at this time.
0 commit comments