LC#231 : Power of Two
Leet Code Maths

25 minutes


go back go back go back home home

LeetCode Problem #231 : Power of Two


Question:

Given an integer n, return true if it is a power of two. Otherwise, return false. An integer n is a power of two, if there exists an integer x such that n == 2x.

Constraints:

(-2 31 ) < n < (2 31 - 1)

Initial Solution:

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

Peformance:

BeatsRuntimeMemory
85.11% Java Users96.43% Java Users
1ms39.04 MB