Open tailgo opened 5 years ago
2的幂在二进制表示下最高位为1其他位为0的数字,这样就很多方法都可以了。 由于平台给的运算时间波动贼大,所以这题就不给参考指标了。
/** * @param {number} n * @return {boolean} */ var isPowerOfTwo = function(n) { // return n > 0 && !(n & (n - 1)); return n > 0 && n.toString(2).replace(/0/g, '') == '1'; };
2的幂在二进制表示下最高位为1其他位为0的数字,这样就很多方法都可以了。 由于平台给的运算时间波动贼大,所以这题就不给参考指标了。