326. Power of Three

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;
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,365评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,625评论 0 23
  • 曾经有个人告诉过我,无论是什么情况什么时候你都不可以哭,因为哭代表了你的懦弱,如果你承认你懦弱,你就哭吧!没人会阻...
    蒲草芗蕴阅读 1,459评论 0 0
  • 莎伦 2010-1-31 23:52 我还是很激动,带的孩子们也长大了,今天终于把筹划了半年的这个工程给顺利完成了...
    lijutong_010阅读 1,562评论 0 0

友情链接更多精彩内容