leetcode_326_Power of Three(easy)(C++)

来源:互联网 发布:日本高中留学 知乎 编辑:程序博客网 时间:2024/06/05 18:04

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?

思路:

在一个double类型的数字上加0.5取整强制转换成int类型就是取下整或上整

class Solution {
public:
    bool isPowerOfThree(int n) {
        double a = log(n)/log(3);
        int b = (int)(a + 0.5);
        if(fabs(a-b) < 1e-10)
            return true;
        return false;
    }
};

0 0
原创粉丝点击