File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n
3
+ * @return {number }
4
+ */
5
+ var hammingWeight = function ( n ) {
6
+ const binary = n . toString ( 2 ) ;
7
+ const arr = [ ...binary ] ;
8
+ let count = 0 ;
9
+ arr . map ( ( num ) => {
10
+ if ( num === "1" ) {
11
+ count ++ ;
12
+ }
13
+ } ) ;
14
+ return count ;
15
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {boolean }
4
+ */
5
+ var isPalindrome = function ( s ) {
6
+ const arr = [ ...s ] ;
7
+ let text = "" ;
8
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
9
+ if ( isAlphabet ( arr [ i ] ) === true && arr [ i ] !== " " ) {
10
+ text = text + arr [ i ] . toLowerCase ( ) ;
11
+ } else if ( isNumeric ( arr [ i ] ) === true && arr [ i ] !== " " ) {
12
+ text = text + arr [ i ] ;
13
+ }
14
+ }
15
+ console . log ( "text -->" , text ) ;
16
+ for ( let i = 0 ; i < text . length / 2 ; i ++ ) {
17
+ if ( text [ i ] !== text [ text . length - 1 - i ] ) {
18
+ return false ;
19
+ }
20
+ }
21
+ return true ;
22
+ } ;
23
+
24
+ function isAlphabet ( char ) {
25
+ return / [ a - z A - Z ] / . test ( char ) ;
26
+ }
27
+
28
+ function isNumeric ( str ) {
29
+ return ! isNaN ( str ) ;
30
+ }
You can’t perform that action at this time.
0 commit comments