326. Power of Three
Previous323. Number of Connected Components in an Undirected GraphNext329. Longest Increasing Path in a Matrix
Last updated
Last updated
/**
* Time complexity : O(log3(N)). The number of divisions is given by
* that logarithm.
* Space complexity : O(1). We are not using any additional memory.
*/
class Solution {
public boolean isPowerOfThree(int n) {
if(n == 0) {
return false;
}
while(n % 3 == 0) {
n /= 3;
}
return n == 1;
}
}