我看到这个问题,并弹出了这个想法。
解决方案 同时(N%3 == 0){
N / = 3;
}
返回否== 1;
请注意,1是三个零功耗。
编辑:您还需要在循环之前检查为零,作为循环不会终止对n = 0(感谢布鲁诺Rothgiesser)
I saw this question,and pop up this idea.
解决方案while (n % 3 == 0) {
n /= 3;
}
return n == 1;
Note that 1 is the zeroth power of three.
Edit: You also need to check for zero before the loop, as the loop will not terminate for n = 0 (thanks to Bruno Rothgiesser).