Problem Description:
Given an integer, write a function to determine if it is a power of three.
题目大意:
给定一个整数,判断它是不是3的幂
Solutions:
简单的循环,判断最后是否得到1.
Code in C++:
class Solution {
public:
bool isPowerOfThree(int n) {
while(n>1)
{
if(n%3>0) return false;
n/=3;
}
return n==1;
}
};
近期评论