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
13 changes: 13 additions & 0 deletions number-of-1-bits/junzero741.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// TC: O(log n)
// SC: O(1)
function hammingWeight(n: number): number {
const binary = n.toString(2);
Copy link
Contributor

Choose a reason for hiding this comment

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

👍👍

let bits = 0;
for(let i = 0; i < binary.length; i++) {
if(binary[i] === '1') {
bits++;
}
}

return bits;
};
29 changes: 29 additions & 0 deletions valid-palindrome/junzero741.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// TC: O(n)
// SC: O(1)
function isPalindrome(s: string): boolean {

let L = 0;
let R = s.length-1;
Comment on lines +5 to +6
Copy link
Contributor

Choose a reason for hiding this comment

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

직관적인 변수명..! 좋은 것 같아요👍👍👍

Copy link
Contributor

Choose a reason for hiding this comment

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

대문자로 쓰는 것도 좋네요.
배워갑니다.


while(R > 0 && L < s.length - 1 && L <= R) {
Copy link
Contributor

Choose a reason for hiding this comment

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

while 조건이 조금 복잡한 것 같다는 생각이 들었습니다🤔

while (L < R) 로 줄여볼 수도 있을 것 같아요!

L은 항상 0 이상이므로 L < R이면 R > 0은 자동으로 보장되는 것 같습니다.. R의 경우에도 항상 s.length - 1 이하이므로 L < R이면 L < s.length - 1도 같이 포함되어서 조건문을 단순화할 수 있을 것 같습니다🙌

if(!isAlphanumeric(s[L])) {
L++;
continue;
}
if(!isAlphanumeric(s[R])) {
R--;
continue;
}
if(s[L].toLowerCase() !== s[R].toLowerCase()) {
return false;
}
L++;
R--;
}

return true;
};

function isAlphanumeric(str: string): boolean {
return /^[a-zA-Z0-9]+$/.test(str);
}
Loading