-1
问题是要找到一个数字中最大的二进制差距,虽然这在我的IDE中工作,但Codility不接受它。有什么想法吗?这个Codility Answer有什么问题?
const biGap = (number) => {
const binary = number.toString(2)
const re = /1(0+)1/g;
const found = binary.match(re)
let counter = 0
found.map((match) => {
if (match.length > counter) {
counter = match.length
}
})
return (counter - 2);
}
console.log(biGap(1041));
您可以定义 “二元差距”?它只适用于这里的'1'吗? –
它看起来不正确。正则表达式找不到重叠匹配,所以如果输入是“10010000010001”,它将返回包含最长间隔的“1001”和“10001”,但不是“1000001”。 – Barmar
@HunterMcMillen基于他的代码,它似乎是一个数字的二进制表示中的1位之间的一系列0位。 – Barmar