Sunday, June 28, 2015

O(1) Check Power of 2

Using O(1) time to check whether an integer n is a power of 2.
Have you met this question in a real interview?
Yes
Example
For n=4, return true;
For n=5, return false;
Challenge
O(1) time
Tags Expand  
class Solution {
    /*
     * @param n: An integer
     * @return: True or false
     */
    public boolean checkPowerOf2(int n) {
        // write your code here
        if(n < 0) return false;
        if(n == 0) return false;
        return (n & (n - 1)) == 0;
    }
};

No comments:

Post a Comment