Binary gap
1 min readJul 13, 2019
- binary = 이진법의
- binary gap — 1041 = 10000010001
- 여기서 바이너리 갭은 5 — 1과 1사이의 연속되는 0이 최대치 갯수.
- A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
- Iterators_and_Generators
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
- Math.max();
console.log(Math.max(1, 3, 2));
// expected output: 3//for..of
let iterable = [10, 20, 30];
for (let value of iterable) {
value += 1;
console.log(value);
}
// 11
// 21
// 31
function solution(N)
function solution(N) {
// write your code in JavaScript (Node.js 8.9.4) let str = N.toString(2),
zeroCount=0,
result = 0;
console.log('binary',str)
for (let value of str){
if(value ==='0'){
zeroCount+=1;
console.log('zc',zeroCount)
}else{
console.log('result',result)
console.log('zero',zeroCount)
result = Math.max(result,zeroCount);
zeroCount = 0;
}
}
return result;
}
- N을 이진수로 만듬 N.toString(2),
- 0일때 카운팅 시작 zeroCount+=1;
- 1을 만났을때 0카운팅한 합과 result 값을 비교하여 result 값에 넣음Math.max(result,zeroCount);
- 0의 최대갯수를 구했기에 zerocount 0으로 만듬.
- result 출력