A very straight forward solution using Java


  • 1
    N
    public boolean isPowerOfTwo(int n) {
        
        if(n <= 0){
            return false;
        }
        
        if(n == 1){
            return true;
        }
        
        while(n >= 2){
            int mod = n % 2;
            if(mod != 0){
                return false;
            }
            n = n / 2;
        }
        
        return true;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.