###### [leetcode] 326. Power of Three

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

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);
}

}
};

#### <LeetCode OJ> 326. Power of Three

2016-01-08 22:44:49

#### 【Leetcode】：326. Power of Three 问题 in JAVA

2016-05-07 15:46:03

#### [LeetCode 326] Power of Three

2016-01-13 17:11:38

#### leetcode 326 Power of Three C++

2016-05-23 15:50:09

#### 326. Power of Three [easy] (Python)

2016-05-26 11:38:59

#### [python语法巩固][leetcode326][Power of Three]

2016-01-09 21:05:04

#### Leetcode:326. Power of Three(JAVA)

2016-03-03 16:51:32

#### LeetCode ||326. Power of Three

2016-02-24 16:49:21

#### [LeetCode]--326. Power of Three

2016-10-16 17:43:50

#### LeetCode 326. Power of Three

2016-04-21 02:12:38

## 不良信息举报

[leetcode] 326. Power of Three