File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ // Time complexity: O(k)
3+ // Space complexity: O(k)
4+
5+ /**
6+ * @param {number } n
7+ * @return {number }
8+ */
9+
10+ // λ¬Έμμ΄ λ³νμ μ¬μ©ν νμ΄
11+ // μ΄μ§μλ‘ λ³νλ μλ₯Ό λ¬Έμμ΄λ‘ λ³νν 1μ κ°μλ₯Ό μΈλ λ°©λ²
12+ // λ¬Έμμ΄ λ³νμ λΉνΈ μ°μ°μλ³΄λ€ λ리μ§λ§ μ΄ν΄νκΈ° μ¬μ΄ λ°©λ²
13+ var hammingWeight = function ( n ) {
14+ return n . toString ( 2 ) . split ( '' ) . filter ( b => b === '1' ) . length
15+ }
16+
17+ // Time complexity: O(1)
18+ // Space complexity: O(1)
19+
20+ // λΉνΈ μ°μ°μλ₯Ό μ¬μ©ν νμ΄
21+ // λΉνΈ μ°μ°μλ μ΄μ§μλ‘ λ³νλ μλ₯Ό λΉκ΅νλ μ°μ°μ
22+ // μλ°μ€ν¬λ¦½νΈ μμ§μ΄ μ«μλ₯Ό 32λΉνΈ μ μλ‘ λ³νν CPU μμ€μμ μ°μ°μ μν
23+ var hammingWeight = function ( n ) {
24+ let count = 0 ;
25+ for ( let i = 0 ; i < 32 ; i ++ ) {
26+ if ( ( n & ( 1 << i ) ) !== 0 ) {
27+ count ++ ;
28+ }
29+ }
30+ return count ;
31+ } ;
Original file line number Diff line number Diff line change 1+ // Time Complexity O(n)
2+ // Space Complexity O(n)
3+ var isPalindrome = function ( s ) {
4+ const str = s . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, "" ) . toLowerCase ( ) ;
5+ let left = 0 ;
6+ let right = str . length - 1 ;
7+
8+ while ( left < right ) {
9+ if ( str [ left ] !== str [ right ] ) return false ;
10+ left ++ ;
11+ right -- ;
12+ }
13+
14+ return true ;
15+ } ;
16+
17+ console . log ( isPalindrome ( "A man, a plan, a canal: Panama" ) ) ;
18+ console . log ( isPalindrome ( "race a car" ) ) ;
19+ console . log ( isPalindrome ( " " ) ) ;
You canβt perform that action at this time.
0 commit comments