-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerOfFour.java
More file actions
34 lines (29 loc) · 856 Bytes
/
PowerOfFour.java
File metadata and controls
34 lines (29 loc) · 856 Bytes
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
package leetcode;
import java.util.HashSet;
import java.util.Set;
/**
* PowerOfFour
* https://leetcode-cn.com/problems/power-of-four/
* 342. 4的幂
* https://leetcode-cn.com/problems/power-of-four/solution/mei-ju-jie-ti-by-oshdyr-d7ad/
* 题解有O(1)解法: 位判断, 数学特性(mod 3 == 1)
*
* @since 2021-05-31
*/
public class PowerOfFour {
public static void main(String[] args) {
PowerOfFour sol = new PowerOfFour();
System.out.println(sol.isPowerOfFour(1));
System.out.println(sol.isPowerOfFour(5));
System.out.println(sol.isPowerOfFour(16));
}
public boolean isPowerOfFour(int n) {
Set<Integer> all = new HashSet<>();
int base = 1;
for (int i = 0; i < 16; i++) {
all.add(base);
base *= 4;
}
return all.contains(n);
}
}