• LeetCode-326 Power of Three


    题目描述

    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;
        }
    };
  • 相关阅读:
    10月20日动手动脑
    10月20日
    10月19日
    10月18日
    10月17日
    10月16日
    10月15日
    10月14日
    jQuery选择器大全
    面试总结
  • 原文地址:https://www.cnblogs.com/heyn1/p/11189103.html
Copyright © 2020-2023  润新知