-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0231_power_of_two.java
More file actions
64 lines (50 loc) · 1.12 KB
/
0231_power_of_two.java
File metadata and controls
64 lines (50 loc) · 1.12 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
59
60
61
62
63
64
/*
* https://leetcode-cn.com/problems/power-of-two/
给定一个整数,编写一个函数来判断它是否是 2 的幂次方。
示例 1:
输入: 1
输出: true
解释: 2^0 = 1
示例 2:
输入: 16
输出: true
解释: 2^4 = 16
示例 3:
输入: 218
输出: false
---------------------------------------------------------------------------------
Given an integer, write a function to determine if it is a power of two.
Example 1:
Input: 1
Output: true
Explanation: 2^0 = 1
Example 2:
Input: 16
Output: true
Explanation: 2^4 = 16
Example 3:
Input: 218
Output: false
*/
class MySolution {
public boolean isPowerOfTwo(int n) {
if (n <= 0) return false;
while (n >= 2) {
if (n % 2 != 0)
return false;
n = n / 2;
}
return true;
}
}
class Solution1 {
public boolean isPowerOfTwo(int n) {
if (n == Integer.MIN_VALUE) return false;
return n == 0 ? false : (n & (-n)) == n;
}
}
class Solution2 {
public boolean isPowerOfTwo(int n) {
return n > 0 && ((n & (n - 1)) == 0);
}
}