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
16 changes: 16 additions & 0 deletions number-of-1-bits/casentino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function hammingWeight(n: number): number {
let num = n;
let output = 0;
while (num !== 1) {
if (num % 2 === 1) {
output += 1;
num = (num - 1) / 2;
} else {
num = num / 2;
Copy link
Contributor

Choose a reason for hiding this comment

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

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

}
}
if (num === 1) {
output += 1;
}
return output;
}
38 changes: 38 additions & 0 deletions valid-palindrome/casentino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function isPalindrome(s: string): boolean {
let start = 0;
let last = s.length - 1;
while (start < last) {
while (start < last && !isAlphanumeric(s.charAt(start))) {
start += 1;
}
while (start < last && !isAlphanumeric(s.charAt(last))) {
last -= 1;
}
if (s.charAt(start).toLowerCase() !== s.charAt(last).toLowerCase()) {
return false;
}
start += 1;
last -= 1;
}
return true;
}

function isAlphanumeric(character: string) {
const characterCode = character.charCodeAt(0);
const lowerStart = "a".charCodeAt(0);
const lowerEnd = "z".charCodeAt(0);
const upperStart = "A".charCodeAt(0);
const upperEnd = "Z".charCodeAt(0);
const numericStart = "0".charCodeAt(0);
const numericEnd = "9".charCodeAt(0);
if (upperStart <= characterCode && upperEnd >= characterCode) {
return true;
}
if (lowerStart <= characterCode && lowerEnd >= characterCode) {
return true;
}
if (numericStart <= characterCode && numericEnd >= characterCode) {
Copy link
Contributor

Choose a reason for hiding this comment

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

직관적이고 가독성 좋은 two pointer 풀이인 것 같습니다!

추가로 return true 조건들을 한 번에 or 조건으로 비교한 값 자체를 반환한다면 더 간결하게 작성할 수 있을 것 같아요!

Copy link
Contributor

Choose a reason for hiding this comment

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

변수로 가독성 좋게 구현하신 것 같습니다 ㅎㅎ

return true;
}
return false;
}