-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerofTwo.java
More file actions
44 lines (40 loc) · 957 Bytes
/
PowerofTwo.java
File metadata and controls
44 lines (40 loc) · 957 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
35
36
37
38
39
40
41
42
43
44
package easy;
/**
*
* ClassName: PowerofTwo
* @author chenyiAlone
* Create Time: 2019/02/28 20:38:56
* Description: No.231
* Given an integer, write a function to determine if it is a power of two.
Example 1:
Input: 1
Output: true
Explanation: 20 = 1
Example 2:
Input: 16
Output: true
Explanation: 24 = 16
Example 3:
Input: 218
Output: false
*/
public class PowerofTwo {
// fast
public boolean isPowerOfTwo(int n) {
if (n <=0 ) return false;
return (n & (n - 1)) == 0;
}
// slow
public boolean isPowerOfTwoSlow(int n) {
long i = 1;
while (i < n) {
i <<= 1;
}
if (i == n) return true;
else return false;
}
public static void main(String[] args) {
int n = 1073741825;
System.out.println(new PowerofTwo().isPowerOfTwo(n));
}
}