diff --git a/number-of-1-bits/seongmin36.js b/number-of-1-bits/seongmin36.js new file mode 100644 index 000000000..f10254aa0 --- /dev/null +++ b/number-of-1-bits/seongmin36.js @@ -0,0 +1,24 @@ +/** +toString(2)으로 해결했다가 다른 분의 풀이를 보고 다시 풀어보았다. + +'비트 연산을 통한 계산'이다. +컴퓨터에서 모든 언어는 2진법으로 변환이 되는데, 이를 이용한 플이다. +가장 오른쪽 비트가 1인 경우에 count++, n을 우측으로 1칸 shift. +shift를 하면 결국 남는 숫자는 0이기 때문에 적절한 조건으로 루프를 빠져나온다. + +굳이 2진법으로 변환하지 않고도 계산할 수 있어서 이게 더 좋은 풀이라 생각하였다. + */ +/** + * @param {number} n + * @return {number} + */ +function hammingWeight(n) { + let count = 0; + + while (n !== 0) { + count += n & 1; + n >>>= 1; + } + + return count; +} diff --git a/valid-palindrome/seongmin36.js b/valid-palindrome/seongmin36.js new file mode 100644 index 000000000..dedd19fc6 --- /dev/null +++ b/valid-palindrome/seongmin36.js @@ -0,0 +1,27 @@ +/** +s를 정규식으로 알파벳 소문자 string만 남겨둬야한다. +알파벳 소문자만 남겨놓도록 하는 정규식은 '/[^a-z0-9]/gi'이다. +left(0)와 right(마지막 인덱스)를 동시에 하나씩 줄여가면서 비교 + */ + +/** + * @param {string} s + * @return {boolean} + */ +function isPalindrome(s) { + s = s.replace(/[^a-z0-9]/gi, "").toLowerCase(); + + let left = 0; + let right = s.length - 1; + + while (left < right) { + if (s[left] !== s[right]) { + return false; + } + + left++; + right--; + } + + return true; +}