File tree Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์๊ฐ ๋ณต์ก๋: O(log n)
3
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(n)
4
+ */
5
+ //
6
+ var hammingWeight = function ( n ) {
7
+ let count = 0 ;
8
+
9
+ while ( n > 0 ) {
10
+ if ( n % 2 ) count += 1 ;
11
+
12
+ // Math.floor๋ ๊ฐ์ ์ญํ
13
+ n = ~ ~ ( n / 2 ) ;
14
+ }
15
+
16
+ return count ;
17
+ } ;
18
+
19
+ // ๋นํธ ์ฐ์ฐ์ ์ฌ์ฉ
20
+ var hammingWeight = function ( n ) {
21
+ let count = 0 ;
22
+ while ( num > 0 ) {
23
+ if ( n & 1 ) count += 1 ;
24
+ n >>>= 1 ;
25
+ }
26
+
27
+ return countOne ;
28
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์๊ฐ ๋ณต์ก๋: O(n)
3
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(n)
4
+ */
5
+ // var isPalindrome = function (s) {
6
+ // // ์ซ์์ ๋ฌธ์๋ง ์ถ์ถํ๋ ์ ๊ท์
7
+ // const REGEX = /[0-9a-zA-Z]/g;
8
+
9
+ // const wordArr = s.match(REGEX);
10
+
11
+ // // ๋ฌธ์๊ฐ ๋น์ด์์ผ๋ฉด true๋ฐํ
12
+ // if (!wordArr) return true;
13
+
14
+ // let l = 0;
15
+ // let r = wordArr.length - 1;
16
+
17
+ // while (l < r) {
18
+ // if (wordArr[l].toLocaleLowerCase() !== wordArr[r].toLocaleLowerCase()) return false;
19
+ // console.log(l, r);
20
+ // l += 1;
21
+ // r -= 1;
22
+ // }
23
+
24
+ // return true;
25
+ // };
26
+
27
+ /**
28
+ * ์๊ฐ ๋ณต์ก๋: O(n)
29
+ * ๊ณต๊ฐ ๋ณต์ก๋: 1
30
+ */
31
+ var isPalindrome = function ( s ) {
32
+ // ์ซ์์ ๋ฌธ์๋ง ์ถ์ถํ๋ ์ ๊ท์
33
+ const isAlnum = ( char ) =>
34
+ ( char >= 'a' && char <= 'z' ) || ( char >= 'A' && char <= 'Z' ) || ( char >= '0' && char <= '9' ) ;
35
+
36
+ let l = 0 ;
37
+ let r = s . length - 1 ;
38
+
39
+ while ( l < r ) {
40
+ console . log ( l , r ) ;
41
+ if ( ! isalnum . test ( s [ l ] ) ) {
42
+ l += 1 ;
43
+ continue ;
44
+ }
45
+ if ( ! isalnum . test ( s [ r ] ) ) {
46
+ r -= 1 ;
47
+ continue ;
48
+ }
49
+ if ( s [ l ] . toLowerCase ( ) !== s [ r ] . toLowerCase ( ) ) {
50
+ return false ;
51
+ }
52
+ l += 1 ;
53
+ r -= 1 ;
54
+ }
55
+
56
+ return true ;
57
+ } ;
58
+
59
+ const s = 'A man, a plan, a canal: Panama' ;
60
+
61
+ console . log ( isPalindrome ( s ) ) ;
You canโt perform that action at this time.
0 commit comments