Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
//给定一个整数,编写函数判断它是否为3的幂
class Solution {
public:
bool isPowerOfThree(int n) {
if(n>1)
{
while(n%3==0)
n=n/3;
}
return n==1;
}
};
判断整数是否为3的幂

本文介绍了一个简单的算法,用于判断一个整数是否可以表示为3的幂。通过不断除以3并检查余数的方式,最终确定该整数是否符合要求。
327

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



