-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0326.3-的幂.java
72 lines (71 loc) · 1.07 KB
/
0326.3-的幂.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* @lc app=leetcode.cn id=326 lang=java
*
* [326] 3 的幂
*
* https://leetcode.cn/problems/power-of-three/description/
*
* algorithms
* Easy (50.73%)
* Likes: 261
* Dislikes: 0
* Total Accepted: 172.3K
* Total Submissions: 339.7K
* Testcase Example: '27'
*
* 给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 true ;否则,返回 false 。
*
* 整数 n 是 3 的幂次方需满足:存在整数 x 使得 n == 3^x
*
*
*
* 示例 1:
*
*
* 输入:n = 27
* 输出:true
*
*
* 示例 2:
*
*
* 输入:n = 0
* 输出:false
*
*
* 示例 3:
*
*
* 输入:n = 9
* 输出:true
*
*
* 示例 4:
*
*
* 输入:n = 45
* 输出:false
*
*
*
*
* 提示:
*
*
* -2^31 <= n <= 2^31 - 1
*
*
*
*
* 进阶:你能不使用循环或者递归来完成本题吗?
*
*/
// @lc code=start
class Solution {
public boolean isPowerOfThree(int n) {
while (n != 0 && n % 3 == 0)
n /= 3;
return n == 1;
}
}
// @lc code=end