25 minutes
Given an integer n, return true if it is a power of three. Otherwise, return false. An integer n is a power of three, if there exists an integer x such that n == 3x.
(-2 31 ) < n < (2 31 - 1)
class Solution {
public boolean isPowerOfThree(int n) {
if(n <= 0) return false;
while(n > 1){
if(n%3 != 0) return false;
n /=3;
}
return true;
}
}
| Beats | Runtime | Memory |
|---|---|---|
| 75.10% Java Users | 29.06% Java Users | |
| 8ms | 42.96 MB |