My Java Solution using while


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

Log in to reply
 

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