Leetcode--326. Power of Three

来源:互联网 发布:html5权威指南 源码 编辑:程序博客网 时间:2024/05/21 17:34

题目

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?

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
不用循环或递归,判断一个数是否是3的x次方

思路

一个基本的事实就是如果n是3的x次方,那么以3为低对数后一定是一个整数,否则不是

代码

class Solution {public:    bool isPowerOfThree(int n) {        double res = log10(n) / log10(3);        return (res-(int)(res)) == 0 ? true : false;    }};
原创粉丝点击