-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerOfTwo.java
80 lines (64 loc) · 1.91 KB
/
PowerOfTwo.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
73
74
75
76
77
78
79
80
package math.easy;
/***
* Problem 231 in Leetcode: https://leetcode.com/problems/power-of-two/
*
* Given an integer n, return true if it is a power of two. Otherwise, return false.
* An integer n is a power of two, if there exists an integer x such that n == 2x.
*
* Example 1:
* Input: n = 0
* Output: false
*
* Example 2:
* Input: n = 16
* Output: true
*
* Example 3:
* Input: n = 2147483647
* Output: false
*/
public class PowerOfTwo {
public static void main(String[] args) {
int number = 1024;
System.out.println("Loop Approach: " + isPowerOfTwoLoop(number));
System.out.println("Recursion Approach: " + isPowerOfTwoRecursion(number));
System.out.println("Logarithm Approach: " + isPowerOfTwoLogarithm(number));
System.out.println("Bitwise Approach: " + isPowerOfTwoBitwise(number));
}
private static boolean isPowerOfTwoLoop(int number) {
if (number <= 0) {
return false;
}
while (number > 2) {
if (isOdd(number)) {
return false;
}
number >>= 1;
}
return true;
}
private static boolean isPowerOfTwoRecursion(int number) {
if (number <= 0) {
return false;
}
if (number == 1) {
return true;
}
return !isOdd(number);
}
private static boolean isPowerOfTwoLogarithm(int number) {
if (number <= 0) {
return false;
}
int power = (int) (Math.log(number) / Math.log(2));
return Math.pow(2, power) == number;
}
private static boolean isPowerOfTwoBitwise(int number) {
boolean isGreaterThanZero = number > 0;
boolean isAndOperationZero = (number & (number - 1)) == 0;
return isGreaterThanZero && isAndOperationZero;
}
private static boolean isOdd(int number) {
return (number & 1) == 1;
}
}