Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 15 additions & 0 deletions number-of-1-bits/youngduck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number} n
* @return {number}
*/
var hammingWeight = function (n) {
// 이진수 변환함수 시간복잡도: O(1)
const bin = n.toString(2);

// replace, replaceAll 시간복잡도: O(n)
const result = bin.replaceAll('0', '').length;

// 시간복잡도: O(n), 공간복잡도: O(1)
Copy link
Contributor

@sonjh1217 sonjh1217 Aug 6, 2025

Choose a reason for hiding this comment

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

오 replace 생각하지 못했는데 좋네용! 그리구 이진이라서 O(logn)이나.. 심지어 O(1) 으로도 풀이가 가능하더라구요. 다른 풀이들도 도전해보시면 좋을 것 같습니다.

Copy link
Contributor

Choose a reason for hiding this comment

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

내장 함수를 잘 사용해 주셨네요! 비트 연산으로도 풀어보시면 어떤 언어에서도 풀이 가능하니 도전해보시면 좋을것 같습니다!


return result;
};
20 changes: 20 additions & 0 deletions valid-palindrome/youngduck.js
Copy link
Contributor

Choose a reason for hiding this comment

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

헉.. 정규식..멋져요

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function (s) {
// 1. toLowerCase() - O(n)
// 2. replace(/[^a-z0-9]/g, '') - O(n)
const clean = s.toLowerCase().replace(/[^a-z0-9]/g, '');

// 3. [...clean] 스프레드 연산자 - O(n)
// 4. reverse() - O(n)
// 5. join('') - O(n)
const reverse = [...clean].reverse().join('');

// 6. 문자열 비교 - O(n)
return clean === reverse;
};

// 시간복잡도 O(n)
// 공간복잡도 O(n)