diff --git a/number-of-1-bits/seungseung88.js b/number-of-1-bits/seungseung88.js new file mode 100644 index 000000000..fe52a4243 --- /dev/null +++ b/number-of-1-bits/seungseung88.js @@ -0,0 +1,28 @@ +/** + * 시간 복잡도: O(log n) + * 공간 복잡도: O(n) + */ +// +var hammingWeight = function (n) { + let count = 0; + + while (n > 0) { + if (n % 2) count += 1; + + // Math.floor랑 같은 역할 + n = ~~(n / 2); + } + + return count; +}; + +// 비트 연산자 사용 +var hammingWeight = function (n) { + let count = 0; + while (num > 0) { + if (n & 1) count += 1; + n >>>= 1; + } + + return countOne; +}; diff --git a/valid-palindrome/seungseung88.js b/valid-palindrome/seungseung88.js new file mode 100644 index 000000000..20a759eea --- /dev/null +++ b/valid-palindrome/seungseung88.js @@ -0,0 +1,61 @@ +/** + * 시간 복잡도: O(n) + * 공간 복잡도: O(n) + */ +// var isPalindrome = function (s) { +// // 숫자와 문자만 추출하는 정규식 +// const REGEX = /[0-9a-zA-Z]/g; + +// const wordArr = s.match(REGEX); + +// // 문자가 비어있으면 true반환 +// if (!wordArr) return true; + +// let l = 0; +// let r = wordArr.length - 1; + +// while (l < r) { +// if (wordArr[l].toLocaleLowerCase() !== wordArr[r].toLocaleLowerCase()) return false; +// console.log(l, r); +// l += 1; +// r -= 1; +// } + +// return true; +// }; + +/** + * 시간 복잡도: O(n) + * 공간 복잡도: 1 + */ +var isPalindrome = function (s) { + // 숫자와 문자만 추출하는 정규식 + const isAlnum = (char) => + (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || (char >= '0' && char <= '9'); + + let l = 0; + let r = s.length - 1; + + while (l < r) { + console.log(l, r); + if (!isalnum.test(s[l])) { + l += 1; + continue; + } + if (!isalnum.test(s[r])) { + r -= 1; + continue; + } + if (s[l].toLowerCase() !== s[r].toLowerCase()) { + return false; + } + l += 1; + r -= 1; + } + + return true; +}; + +const s = 'A man, a plan, a canal: Panama'; + +console.log(isPalindrome(s));