Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions number-of-1-bits/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def hammingWeight(self, n: int) -> int:
result = 0
for i in range(32):
if (n >> i) & 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bit shifting을 이용하여 bit manipulation 문제의 의도를 잘 파악하신 것 같습니다!

추가로 n & n-1로 오른쪽에서부터 1인 비트를 하나씩 지워가며 개수를 세는 방식인 Brian Kernighan's 알고리즘이라는 방법도 있어 공유드립니다~!

https://leetcode.com/problems/number-of-1-bits/solutions/4341511/faster-lesser-3-methods-simple-count-brian-kernighan-s-algorithm-bit-manipulation-explained

result += 1
return result
14 changes: 14 additions & 0 deletions valid-palindrome/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
s = [c.lower() for c in s if c.isalnum()]

if len(s) == 1:
return True

l, r = 0, len(s) - 1
while l < r:
if s[l] != s[r]:
return False
l += 1
r -= 1
return True