Skip to content

Commit 866efd8

Browse files
committed
solve 326.power-of-three
1 parent 55fef29 commit 866efd8

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

vscode/326.power-of-three.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @lc app=leetcode id=326 lang=java
3+
*
4+
* [326] Power of Three
5+
*
6+
* https://leetcode.com/problems/power-of-three/description/
7+
*
8+
* algorithms
9+
* Easy (41.40%)
10+
* Total Accepted: 178.1K
11+
* Total Submissions: 428.8K
12+
* Testcase Example: '27'
13+
*
14+
* Given an integer, write a function to determine if it is a power of three.
15+
*
16+
* Example 1:
17+
*
18+
*
19+
* Input: 27
20+
* Output: true
21+
*
22+
*
23+
* Example 2:
24+
*
25+
*
26+
* Input: 0
27+
* Output: false
28+
*
29+
* Example 3:
30+
*
31+
*
32+
* Input: 9
33+
* Output: true
34+
*
35+
* Example 4:
36+
*
37+
*
38+
* Input: 45
39+
* Output: false
40+
*
41+
* Follow up:
42+
* Could you do it without using any loop / recursion?
43+
*/
44+
class Solution {
45+
public boolean isPowerOfThree(int n) {
46+
if (n < 1) return false;
47+
while (n % 3 == 0) {
48+
n /= 3;
49+
}
50+
return n == 1;
51+
}
52+
}
53+

0 commit comments

Comments
 (0)