Skip to content

Commit a81d487

Browse files
authored
Merge pull request #1303 from b41-41/main
2 parents 124f3a6 + 46aac57 commit a81d487

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

โ€Žnumber-of-1-bits/b41-41.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function hammingWeight(n: number): number {
2+
3+
// ํ’€์ด 1:
4+
// binary๋กœ ๋ณ€ํ™˜ ํ›„ 1 count
5+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(log n)
6+
// ๊ณต๊ฐ„ ๋ณต์žก๋„: O(log n)
7+
const getResult1 = () => {
8+
const binaryNum = n.toString(2);
9+
10+
let count = 0;
11+
12+
for(let i = 0; i < binaryNum.length; i++) {
13+
if(binaryNum.charAt(i) === '1') {
14+
count++
15+
}
16+
}
17+
18+
return count;
19+
};
20+
21+
// ํ’€์ด 2:
22+
// ๋น„ํŠธ ์—ฐ์‚ฐ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•จ (From GPT)
23+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(log n)
24+
// ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
25+
// const getResult2 = () => {
26+
// let count = 0;
27+
28+
// while (n !== 0) {
29+
// count += n & 1;
30+
// n >>>= 1;
31+
// }
32+
33+
// return count;
34+
// }
35+
36+
return getResult1();
37+
// return getResult2();
38+
};

โ€Žvalid-palindrome/b41-41.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function isPalindrome(s: string): boolean {
2+
3+
// ํ’€์ด 1:
4+
// lowcase ๋ณ€ํ™˜, ์ก๊ทœ์‹์œผ๋กœ ๋ฌธ์ž ์ด์™ธ ํ•„ํ„ฐ
5+
// ํ•„ํ„ฐ๋œ ๋ฌธ์ž์—ด ์ˆœํšŒํ•˜๋ฉด์„œ (i, length - i)
6+
// ์ „๋ถ€ ์ผ์น˜ํ•˜๋ฉด true ์•„๋‹ˆ๋ฉด false
7+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
8+
// ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
9+
10+
const validPalindrome1 = () => {
11+
const sanitizedStrArr = [...s.toLowerCase().replace(/[^a-z0-9]/g, "")];
12+
13+
for(let i = 0; i < Math.floor(sanitizedStrArr.length / 2); i++) {
14+
if(sanitizedStrArr[i] !== sanitizedStrArr[(sanitizedStrArr.length - 1) - i]) {
15+
return false;
16+
}
17+
}
18+
19+
return true;
20+
}
21+
22+
return validPalindrome1();
23+
};

0 commit comments

Comments
ย (0)