231. Power of Two


做题历程:

  1. 应该做了不止2次了,本次独立解出,大概耗时6分钟

难度挺低的,尤其是做了几次之后。这次是用了一个n & (n - 1) = 0x0的trick。。。直接上代码吧。

class Solution {
public:
    bool isPowerOfTwo(int n) {
        return !(n & (n - 1)) && n > 0;
    }
};

results matching ""

    No results matching ""