File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // TC: O(log n)
2+ // SC: O(1)
3+ function hammingWeight ( n : number ) : number {
4+ const binary = n . toString ( 2 ) ;
5+ let bits = 0 ;
6+ for ( let i = 0 ; i < binary . length ; i ++ ) {
7+ if ( binary [ i ] === '1' ) {
8+ bits ++ ;
9+ }
10+ }
11+
12+ return bits ;
13+ } ;
Original file line number Diff line number Diff line change 1+ // TC: O(n)
2+ // SC: O(1)
3+ function isPalindrome ( s : string ) : boolean {
4+
5+ let L = 0 ;
6+ let R = s . length - 1 ;
7+
8+ while ( R > 0 && L < s . length - 1 && L <= R ) {
9+ if ( ! isAlphanumeric ( s [ L ] ) ) {
10+ L ++ ;
11+ continue ;
12+ }
13+ if ( ! isAlphanumeric ( s [ R ] ) ) {
14+ R -- ;
15+ continue ;
16+ }
17+ if ( s [ L ] . toLowerCase ( ) !== s [ R ] . toLowerCase ( ) ) {
18+ return false ;
19+ }
20+ L ++ ;
21+ R -- ;
22+ }
23+
24+ return true ;
25+ } ;
26+
27+ function isAlphanumeric ( str : string ) : boolean {
28+ return / ^ [ a - z A - Z 0 - 9 ] + $ / . test ( str ) ;
29+ }
You can’t perform that action at this time.
0 commit comments