-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerOfThree.java
More file actions
58 lines (52 loc) · 1.51 KB
/
PowerOfThree.java
File metadata and controls
58 lines (52 loc) · 1.51 KB
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
package com.leetcode.bits;
public class PowerOfThree {
/***
* 给定一个整数,写一个函数来判断它是否是 3的幂次方。
* 示例 1:
* 输入: 27
* 输出: true
* 示例 2:
* 输入: 0
* 输出: false
* 示例 3:
* 输入: 9
* 输出: true
* 示例 4:
* 输入: 45
* 输出: false
* 进阶:
* 你能不使用循环或者递归来完成本题吗?
* 链接:https://leetcode-cn.com/problems/power-of-three
*/
public boolean isPowerOfThree(int n) {
switch (n){
case 1:return true;
case 3:return true;
case 9:return true;
case 27:return true;
case 81:return true;
case 243:return true;
case 729:return true;
case 2187:return true;
case 6561:return true;
case 19683:return true;
case 59049:return true;
case 177147:return true;
case 531441:return true;
case 1594323:return true;
case 4782969:return true;
case 14348907:return true;
case 43046721:return true;
case 129140163:return true;
case 387420489:return true;
case 1162261467:return true;
default:return false;
}
}
public static void main(String[] args) {
for(int i=1;i<30;i++){
System.out.println(Math.pow(3,i));
}
//System.out.println(Integer.MAX_VALUE);
}
}