Problem
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?
Example
Input: 27
Output: true
Input: 0
Output: false
Input: 9
Output: true
Input: 45
Output: false
Code
static int var = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
bool isPowerOfThree(int n) {
return n>0 && 1162261467%n==0;
}
};
网友评论