File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ function hammingWeight ( n : number ) : number {
2
+ let count = 0 ;
3
+ let num = n ;
4
+
5
+ while ( num > 0 ) {
6
+ if ( num % 2 === 1 ) {
7
+ count ++ ;
8
+ }
9
+ num = Math . floor ( num / 2 ) ;
10
+ }
11
+
12
+ return count ;
13
+ } ;
Original file line number Diff line number Diff line change
1
+ const alphaNumSet = new Set ( [ ..."abcdefghijklmnopqrstuvwxyz0123456789" ] ) ;
2
+
3
+ function isPalindrome ( s : string ) : boolean {
4
+ let normalized = "" ;
5
+
6
+ for ( const characters of s ) {
7
+ const lower = characters . toLowerCase ( ) ;
8
+ if ( alphaNumSet . has ( lower ) ) {
9
+ normalized += lower ;
10
+ }
11
+ }
12
+
13
+ let left = 0 ;
14
+ let right = normalized . length - 1 ;
15
+ while ( left < right ) {
16
+ if ( normalized [ left ] !== normalized [ right ] ) {
17
+ return false ;
18
+ }
19
+ left ++ ;
20
+ right -- ;
21
+ }
22
+
23
+ return true ;
24
+ }
You can’t perform that action at this time.
0 commit comments