Problem: Power of Two

Posted by Marcy on February 18, 2015

Question

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

Solution

TODO

Code

class Solution {
    public boolean isPowerOfTwo(int n) {
        if(n<=0) return false;
        if(n==1) return true;
        
        while(n>=2) {
            if(n%2 == 1) return false;
            n/=2;
        }
        
        return true;
    }
}

Performance

TODO