/** * @param {number} N * @return {number} */ var binaryGap = function(N) { let bytes = N.toString(2) // console.info(`[bytes=:>${JSON.stringify(bytes)}`)
let indexArray = []
for (let i = 0; i < bytes.length; i++) { // console.info(`[bytes[i]]:==:>${JSON.stringify(bytes[i])}`) if (bytes[i] === '1') indexArray.push(i) } if (indexArray.length < 2) return0 let max = 0 for (let i = 0; i < indexArray.length - 1; i++) { max = Math.max(max, indexArray[i + 1] - indexArray[i]) } return max }
/** * @param {number} N * @return {number} */ var binaryGap = function(N) { let bytes = N.toString(2) // console.info(`[bytes=:>${JSON.stringify(bytes)}`)
let indexArray = []
let lastIndex = undefined let max = 0 for (let i = 0; i < bytes.length; i++) { // console.info(`[bytes[i]]:==:>${JSON.stringify(bytes[i])}`) if (bytes[i] === '1') { if (lastIndex >= 0) { max = Math.max(i - lastIndex, max) } lastIndex = i } } return max }