Skip to content

Instantly share code, notes, and snippets.

@kninami
Created October 21, 2016 07:56
Show Gist options
  • Select an option

  • Save kninami/4366115a6712295a4c5a4cf7f107070c to your computer and use it in GitHub Desktop.

Select an option

Save kninami/4366115a6712295a4c5a4cf7f107070c to your computer and use it in GitHub Desktop.
/*
node로 실행
풀이 방식
시간 복잡도를 O(n)으로 하기 위해 bit연산으로 하여 처리
10진수 값을 1과 AND 연산해서 2진수의 역순으로 하나씩 꺼내서 처리
*/
function solution(N) {
let curNum = N;
let maxZeroCnt = 0;
let curZeroCnt = 0;
let isValid = false;
while(true) {
const curBinNum = curNum & 1;
const isVaildZeroSection = isValid && curBinNum === 0;
const isValidBoundary = curBinNum === 1;
if ( isVaildZeroSection ) {
curZeroCnt++;
}
if ( isValidBoundary ) {
isValid = true;
if (maxZeroCnt < curZeroCnt) {
maxZeroCnt = curZeroCnt;
}
curZeroCnt = 0;
}
curNum = curNum >> 1;
if (curNum === 0) {
break;
}
}
return maxZeroCnt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment