tailgo / poorguy-fly

1 stars 0 forks source link

231. 2的幂(简单) - https://leetcode-cn.com/problems/power-of-two/submissions/ #14

Open tailgo opened 5 years ago

tailgo commented 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';
};