diff --git a/number-of-1-bits/JinuCheon.py b/number-of-1-bits/JinuCheon.py new file mode 100644 index 0000000000..70ac8d1990 --- /dev/null +++ b/number-of-1-bits/JinuCheon.py @@ -0,0 +1,25 @@ +# 오래전 배운 2진수 구하는 방법을 그대로 적용. +class Solution: + def hammingWeight(self, n: int) -> int: + cnt = 0 + while n > 0: + remainder = n % 2 + if remainder == 1: + cnt += 1 + n = n // 2 + return cnt + +# LLM 이 알려준 다른 풀이. +# 그렇지만 라이브코딩테스트 등에서는 썩 적합하지 않은듯. +class Solution: + def hammingWeight(self, n: int) -> int: + return bin(n).count('1') + +# 또 다른 풀이. 비트연산 활용. +class Solution: + def hammingWeight(self, n: int) -> int: + count = 0 + while n: + count += n & 1 # 맨 오른쪽 비트가 1인지 확인 + n >>= 1 # 오른쪽으로 한 칸 시프트 + return count \ No newline at end of file diff --git a/valid-palindrome/JinuCheon.py b/valid-palindrome/JinuCheon.py new file mode 100644 index 0000000000..41c6dbb84d --- /dev/null +++ b/valid-palindrome/JinuCheon.py @@ -0,0 +1,31 @@ +# two point 풀이. +# 시간복잡도: O(n) +# 이중루프라서 시간복잡도 계산이 조금 헷갈린다. +# 추가공간을 쓰지 않는다. +class Solution: + def isPalindrome(self, s: str) -> bool: + leftCursor = 0 + rightCursor = len(s) - 1 + + while leftCursor < rightCursor: + while leftCursor < rightCursor and not s[leftCursor].isalnum(): + leftCursor += 1 + + while leftCursor < rightCursor and not s[rightCursor].isalnum(): + rightCursor -= 1 + + if s[leftCursor].lower() != s[rightCursor].lower(): + return False + + leftCursor += 1 + rightCursor -= 1 + + return True + +# 다른 풀이. +# 문자열 클리닝 & 뒤집어서 동등비교를 함. +# 간결하지만 추가공간 사용 -> 공간 O(n) +class Solution: + def isPalindrome(self, s: str) -> bool: + cleaned = ''.join(c.lower() for c in s if c.isalnum()) + return cleaned == cleaned[::-1]