Skip to content

Commit 9fdae26

Browse files
authored
[JinuCheon] WEEK 03 Solutions (#2731)
* 217. Contains Duplicate * Two Sum * valid anagram * climbing stairs * add line break * ๋ฆฌ๋ทฐ ๋ฐ˜์˜: ์ ํ™”์‹ ์ ์šฉํ•˜์—ฌ ์žฌ๊ท€ํ˜ธ์ถœ ์ œ๊ฑฐ * Product of Array Except Self * threeSum * 3์ฃผ์ฐจ * add line break
1 parent 30e11eb commit 9fdae26

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ์˜ค๋ž˜์ „ ๋ฐฐ์šด 2์ง„์ˆ˜ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ทธ๋Œ€๋กœ ์ ์šฉ.
2+
class Solution:
3+
def hammingWeight(self, n: int) -> int:
4+
cnt = 0
5+
while n > 0:
6+
remainder = n % 2
7+
if remainder == 1:
8+
cnt += 1
9+
n = n // 2
10+
return cnt
11+
12+
# LLM ์ด ์•Œ๋ ค์ค€ ๋‹ค๋ฅธ ํ’€์ด.
13+
# ๊ทธ๋ ‡์ง€๋งŒ ๋ผ์ด๋ธŒ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋“ฑ์—์„œ๋Š” ์ฉ ์ ํ•ฉํ•˜์ง€ ์•Š์€๋“ฏ.
14+
class Solution:
15+
def hammingWeight(self, n: int) -> int:
16+
return bin(n).count('1')
17+
18+
# ๋˜ ๋‹ค๋ฅธ ํ’€์ด. ๋น„ํŠธ์—ฐ์‚ฐ ํ™œ์šฉ.
19+
class Solution:
20+
def hammingWeight(self, n: int) -> int:
21+
count = 0
22+
while n:
23+
count += n & 1 # ๋งจ ์˜ค๋ฅธ์ชฝ ๋น„ํŠธ๊ฐ€ 1์ธ์ง€ ํ™•์ธ
24+
n >>= 1 # ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ์‹œํ”„ํŠธ
25+
return count
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# two point ํ’€์ด.
2+
# ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
3+
# ์ด์ค‘๋ฃจํ”„๋ผ์„œ ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ์ด ์กฐ๊ธˆ ํ—ท๊ฐˆ๋ฆฐ๋‹ค.
4+
# ์ถ”๊ฐ€๊ณต๊ฐ„์„ ์“ฐ์ง€ ์•Š๋Š”๋‹ค.
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
leftCursor = 0
8+
rightCursor = len(s) - 1
9+
10+
while leftCursor < rightCursor:
11+
while leftCursor < rightCursor and not s[leftCursor].isalnum():
12+
leftCursor += 1
13+
14+
while leftCursor < rightCursor and not s[rightCursor].isalnum():
15+
rightCursor -= 1
16+
17+
if s[leftCursor].lower() != s[rightCursor].lower():
18+
return False
19+
20+
leftCursor += 1
21+
rightCursor -= 1
22+
23+
return True
24+
25+
# ๋‹ค๋ฅธ ํ’€์ด.
26+
# ๋ฌธ์ž์—ด ํด๋ฆฌ๋‹ & ๋’ค์ง‘์–ด์„œ ๋™๋“ฑ๋น„๊ต๋ฅผ ํ•จ.
27+
# ๊ฐ„๊ฒฐํ•˜์ง€๋งŒ ์ถ”๊ฐ€๊ณต๊ฐ„ ์‚ฌ์šฉ -> ๊ณต๊ฐ„ O(n)
28+
class Solution:
29+
def isPalindrome(self, s: str) -> bool:
30+
cleaned = ''.join(c.lower() for c in s if c.isalnum())
31+
return cleaned == cleaned[::-1]

0 commit comments

Comments
ย (0)