25 minutes
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.
(-2 31 ) < n < (2 31 - 1)
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;
}
}
| Beats | Runtime | Memory |
|---|---|---|
| 85.11% Java Users | 96.43% Java Users | |
| 1ms | 39.04 MB |