forked from algorithm008-class01/algorithm008-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPPowerOfTwo.java
More file actions
39 lines (36 loc) · 819 Bytes
/
Copy pathPPowerOfTwo.java
File metadata and controls
39 lines (36 loc) · 819 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
//给定一个整数,编写一个函数来判断它是否是 2 的幂次方。
//
// 示例 1:
//
// 输入: 1
//输出: true
//解释: 20 = 1
//
// 示例 2:
//
// 输入: 16
//输出: true
//解释: 24 = 16
//
// 示例 3:
//
// 输入: 218
//输出: false
// Related Topics 位运算 数学
package com.leetCode.leetcode.editor.cn;
//Java:2的幂, 231
public class PPowerOfTwo{
public static void main(String[] args) {
Solution solution = new PPowerOfTwo().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isPowerOfTwo(int n) {
if (n <= 0) return false;
n = n - (n & (-n));
return n == 0;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}