Skip to content

[froggy1014] Week 03 solutions #1305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 20, 2025
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
31 changes: 31 additions & 0 deletions number-of-1-bits/froggy1014.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

// Time complexity: O(k)
// Space complexity: O(k)

/**
* @param {number} n
* @return {number}
*/

// 문자열 변환을 사용한 풀이
// 이진수로 변환된 수를 문자열로 변환후 1의 개수를 세는 방법
// 문자열 변환은 비트 연산자보다 느리지만 이해하기 쉬운 방법
var hammingWeight = function (n) {
return n.toString(2).split('').filter(b => b === '1').length
}

// Time complexity: O(1)
// Space complexity: O(1)

// 비트 연산자를 사용한 풀이
// 비트 연산자는 이진수로 변환된 수를 비교하는 연산자
// 자바스크립트 엔진이 숫자를 32비트 정수로 변환후 CPU 수준에서 연산을 수행
var hammingWeight = function (n) {
let count = 0;
for (let i = 0; i < 32; i++) {
if ((n & (1 << i)) !== 0) {
count++;
}
}
return count;
};
19 changes: 19 additions & 0 deletions valid-palindrome/froggy1014.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Time Complexity O(n)
// Space Complexity O(n)
var isPalindrome = function (s) {
const str = s.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
let left = 0;
let right = str.length - 1;

while (left < right) {
if (str[left] !== str[right]) return false;
left++;
right--;
}

return true;
};

console.log(isPalindrome("A man, a plan, a canal: Panama"));
console.log(isPalindrome("race a car"));
console.log(isPalindrome(" "));