Power of Two

LeetCode #231


Description:

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

Example:

Note

Idea:

(n&(n-1)) == 0 ?

Code:

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n<=0) return false;

        return (n&(n-1)) == 0 ? true: false;
    }
};

results matching ""

    No results matching ""