[leetcode] 326. Power of Three

原创 2016年06月02日 10:43:03

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?

check n =? 3^x

Solution 1

Idea: divide 3 to see whether get 0 residue

class Solution {
public:
    bool isPowerOfThree(int n) {
        while (n!=0 && n%3==0){
            n= n/3;
        }
        return n==1;
    }
};

Solution 2

Idea: if n=3^x, then log10(n) = xlog10(3), log10(n)/log10(3) is integer

class Solution {
public:
    bool isPowerOfThree(int n) {
        if (n < 0)
            return false;
        else{
            return (int(log10(n)/log10(3))-(log10(n)/log10(3)) ==0);
        }
        
    }
};



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode326. Power of Three

326. Power of ThreeGiven an integer, write a function to determine if it is a power of three.Follow ...

LeetCode 326. Power of Three 解题报告

LeetCode 326. Power of Three 解题报告

LeetCode笔记:326. Power of Three

判断一个整数是否是3的次方数

【leetcode】326. Power of Three

一、题目描述 Given an integer, write a function to determine if it is a power of three. Follow up:...

<LeetCode OJ> 326. Power of Three

326. Power of Three My Submissions Question Total Accepted: 1159 Total Submissions: 3275 Difficu...

326. Power of Three(C++)

题目:Given an integer, write a function to determine if it is a power of three. 给一个整数,判断它是不是3的幂数 Fo...

Leetcode 231. Power of Two & 326. Power of Three & 342. Power of Four

231. Power of Two   Total Accepted: 69528 Total Submissions: 190898 Difficulty: Easy Given an ...

leetcode: power of three 三种解法

一、题目leetcode 上有这么一道题,power of three.题目如下: Given an integer, write a function to determine if it is...

LeetCode|Power of three

LeetCode Power of Three

leetcode- Number of 1 bits, power of two, power of three

leetcode- Number of 1 bits, power of two, power of three
  • aaasy
  • aaasy
  • 2016-04-23 22:13
  • 73
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)