Skip to content

Commit e43e5ae

Browse files
committed
solve 231: power of two
1 parent 293140c commit e43e5ae

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

vscode/231.power-of-two.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @lc app=leetcode id=231 lang=java
3+
*
4+
* [231] Power of Two
5+
*
6+
* https://leetcode.com/problems/power-of-two/description/
7+
*
8+
* algorithms
9+
* Easy (41.58%)
10+
* Total Accepted: 211.8K
11+
* Total Submissions: 509.3K
12+
* Testcase Example: '1'
13+
*
14+
* Given an integer, write a function to determine if it is a power of two.
15+
*
16+
* Example 1:
17+
*
18+
*
19+
* Input: 1
20+
* Output: true
21+
* Explanation: 20 = 1
22+
*
23+
*
24+
* Example 2:
25+
*
26+
*
27+
* Input: 16
28+
* Output: true
29+
* Explanation: 24 = 16
30+
*
31+
* Example 3:
32+
*
33+
*
34+
* Input: 218
35+
* Output: false
36+
*
37+
*/
38+
class Solution {
39+
public boolean isPowerOfTwo(int n) {
40+
if (n <= 0) return false;
41+
if ((n & (n-1)) == 0) {
42+
return true;
43+
}
44+
return false;
45+
}
46+
}
47+

0 commit comments

Comments
 (0)