326. Power of Three

2018-09-26  本文已影响0人  SilentDawn

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;
    }
};

Result

326. Power of Three.png
上一篇 下一篇

猜你喜欢

热点阅读