LeetCode 326. Power of Three

原创 2016年05月31日 21:44:30

Power of Three

Des

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

分析

这个题开始没理解,以为是求一个数是否是某个数的三次方,导致不过。后来看了网上的才知道是求是否是3的x次方。那么如果一个数是的的肯定就能被3一直整除至到不能再被整除时商是否为1,这就是解题的关键。

Code

class Solution {
public:
    bool isPowerOfThree(int n) {
        while(n&&(n%3==0)) n/=3;
        if(n==1){ 
            return true;
        }
        else{ 
            return false;
        }
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

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 解题报告

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

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
  • 51
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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