LeetCode之326 Power of Three

来源:互联网 发布:软件开发计划书 编辑:程序博客网 时间:2024/05/02 02:19

原题:

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?

AC后的代码(注意,该代码只对素数有用):

class Solution {public:    bool isPowerOfThree(int n) {        //some int =3^k;        //k=log3(some max int)        if(n<=0) return false;        const int maxint =0x7fffffff;        int k=log(maxint)/log(3);                int somgbig3=pow(3,k);        return (somgbig3%n==0);    }};


0 0