1. 程式人生 > >LeetCode 第 231 題 (Power of Two)

LeetCode 第 231 題 (Power of Two)

i++ ret write data mark hid article views pop

LeetCode 第 231 題 (Power of Two)

Given an integer, write a function to determine if it is a power of two.

這個題目有個特別簡單的解法。當然。可以獨自想出這種方法可不簡單。這樣的方法可以作為一個知識點記住就好了。

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n <= 0) return false;
        return !(n & (n - 1));
    }
};
‘).text(i)); }; $numbering.fadeIn(1700); }); });

LeetCode 第 231 題 (Power of Two)