题解 | #求最大连续bit数#
求最大连续bit数
https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
常规套路,先分享闭眼算法,再分享巧妙算法,完整代码如下
//闭眼算法--双重for循环 while (line = readline()) { let n = parseInt(line).toString(2); let max = 0; for (let i = 0; i < n.length; i++) { for (let j = i+1; j <= n.length; j++) { let str = n.slice(i, j); if (!str.includes('0') && str.length > max) { max = str.length; } } } console.log(max); } //巧妙算法--split()方法的妙用 /* while (line = readline()) { let arr = parseInt(line).toString(2).split('0'); let max = Math.max(...arr); console.log(max.toString().length); }*/