首页 > 其他 > 详细

LeetCode-326 Power of Three

时间:2019-07-15 15:28:35      阅读:73      评论:0      收藏:0      [点我收藏+]

题目描述

Given an integer, write a function to determine if it is a power of three.

 

题目大意

判断给的整数是否是3的乘方数。

(尽量不要用循环和递归来完成算法)

 

示例

E1

Input: 27
Output: true

E2

Input: 0
Output: false

E3

Input: 9
Output: true

E4

Input: 45
Output: false

 

解题思路

log10n / log103应该为整数。

 

复杂度分析

时间复杂度:O(1)

空间复杂度:O(1)

 

代码

class Solution {
public:
    bool isPowerOfThree(int n) {
        return fmod(log10(n)/log10(3), 1)==0;
    }
};

 

LeetCode-326 Power of Three

原文:https://www.cnblogs.com/heyn1/p/11189103.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!