提交代码
class Solution {
public boolean isPowerOfThree(int n) {
if(n==0) return false;
return n==1?true:n%3!=0?false:isPowerOfThree(n/3);
}
}
运行结果
除此之外一个大神的一行代码:
public class Solution {
public boolean isPowerOfThree(int n) {
// 1162261467 is 3^19, 3^20 is bigger than int
return ( n>0 && 1162261467%n==0);
}