-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