美文网首页
326. Power of Three

326. Power of Three

作者: a_void | 来源:发表于2016-09-09 19:48 被阅读0次

    Given an integer, write a function to determine if it is a power of three.

    Solution 1:

    class Solution {
    public:
        bool isPowerOfThree(int n) {
            int arr[] = {1,3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147, 531441, 1594323, 4782969, 14348907, 43046721, 129140163, 387420489, 1162261467, 3486784401, 10460353203};
            set<int> s = set<int>(arr, arr+21);
            return s.count(n) == 1;
        }
    };
    

    Solution 2:

    class Solution {
    public:
        bool isPowerOfThree(int n) {
            return n > 0 && 1162261467 % n == 0;
        }
    };
    

    相关文章

      网友评论

          本文标题:326. Power of Three

          本文链接:https://www.haomeiwen.com/subject/astyettx.html