码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Power of Three

时间:2017-07-22 23:53:54      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:amp   最大   any   with   题目   loop   cti   out   target   

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?

判断一个数是否是3的幂,则这个数a的所有的约数都是3的幂,如果一个数b小于这个数a且是3的幂,则这个数b一定是a的约数。所以找出3的最大的幂,然后用这个数对n取余即可。

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

相关题目:Power of Two

相关题目:Power of Four

[LeetCode] Power of Three

标签:amp   最大   any   with   题目   loop   cti   out   target   

原文地址:http://www.cnblogs.com/immjc/p/7222997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!