定义左指针l,和右指针r,要考虑边界的条件,两个指针均小于数组的长度。
定义最大连续的1,为max初始值为0;
如果nums[l] 为1,且nums[r]也为1,更新max = Math.max(max, r - l + 1)
如果不满足,则需要更新左指针的位置,更新为 r + 1,
每次遍历,还需要更新下右指针。
最后返回最大的max即可
定义最大连续的1,为max初始值为0;
如果nums[l] 为1,且nums[r]也为1,更新max = Math.max(max, r - l + 1)
如果不满足,则需要更新左指针的位置,更新为 r + 1,
每次遍历,还需要更新下右指针。
最后返回最大的max即可
const getBigestBit = (num) => {
const bitnum = Number(num).toString(2);
const arr = bitnum.split('')
let l = 0, r = 0;
let max = 0;
let len = arr.length;
while( l < len && r < len){
if(arr[l] === '1' && arr[r]==='1'){
max = Math.max(max,r - l + 1);
}else{
l = r + 1;
}
r++;
}
return max;
}
const readline = require('readline');
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
});
rl.on('line',(line) =>{
console.log(getBigestBit(line))
})
const getBigestBit = (num) => {
let arr = Number(num).toString(2).replace(/[^1]+/g,' ').split(' ');
let max = 0
arr.forEach(item =>{
max = Math.max(max,item.length)
})
return max
}
const readline = require('readline');
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
});
rl.on('line',(line) =>{
console.log(getBigestBit(line))
})

京公网安备 11010502036488号