提交代码
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);
}
本文介绍了一种高效的方法来判断一个整数是否可以表示为3的幂次方。通过递归和利用最大3的幂次方数值,提供了一个简洁的解决方案。
1252

被折叠的 条评论
为什么被折叠?



