Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 additions & 0 deletions number-of-1-bits/hi-rachel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# O(log n) time, O(1) space
# % 나머지, // 몫

class Solution:
def hammingWeight(self, n: int) -> int:
cnt = 0

while n > 0:
if (n % 2) == 1:
cnt += 1
n //= 2

return cnt


# O(log n) time, O(log n) space
class Solution:
def hammingWeight(self, n: int) -> int:
return bin(n).count("1")
Copy link
Contributor

Choose a reason for hiding this comment

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

bin() 함수는 처음 보았는데 흥미롭네요 😃



# TS 풀이
# O(log n) time, O(log n) space
# function hammingWeight(n: number): number {
# return n.toString(2).split('').filter(bit => bit === '1').length;
# };
20 changes: 20 additions & 0 deletions valid-palindrome/hi-rachel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# O(n) time, O(1) space
# isalnum() -> 문자열이 영어, 한글 혹은 숫자로 되어있으면 참 리턴, 아니면 거짓 리턴.

class Solution:
def isPalindrome(self, s: str) -> bool:
l = 0
r = len(s) - 1

while l < r:
if not s[l].isalnum():
l += 1
elif not s[r].isalnum():
r -= 1
elif s[l].lower() == s[r].lower():
l += 1
r -= 1
else:
return False
return True

21 changes: 21 additions & 0 deletions valid-palindrome/hi-rachel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// O(n) time, O(1) space

function isPalindrome(s: string): boolean {
let low = 0,
high = s.length - 1;

while (low < high) {
while (low < high && !s[low].match(/[0-9a-zA-Z]/)) {
low++;
}
while (low < high && !s[high].match(/[0-9a-zA-Z]/)) {
high--;
}
if (s[low].toLowerCase() !== s[high].toLowerCase()) {
return false;
}
low++;
high--;
}
return true;
}