FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/231_Power_of_Two.java at master · jakehoare/leetcode · GitHub
jakehoare
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
51
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
java
/
231_Power_of_Two.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
19 lines (17 loc) · 601 Bytes
Breadcrumbs
leetcode
/
java
/
231_Power_of_Two.java
Copy path
File metadata and controls
19 lines (17 loc) · 601 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
https://leetcode.com/problems/power-of-two/
Given an integer, write a function to determine if it is a power of two.
Subtracting 1 from a power of 2 sets all the bits to the right of the bit that was originally set.
So no bits are set on both n and n-1. If not a power of 2 then the higest bit set in n will also
be set in n-1.
Alternatively, count the number of bits that are set by repeatedly right=shifting.
Time - O(log n)
Space - O(1)
*/
public
class
Solution
{
public
boolean
isPowerOfTwo
(
int
n
) {
if
(
n
<=
0
)
return
false
;
return
(
n
& (
n
-
1
)) ==
0
;
}
}
Back
|
FazBrowse Home
|
New Git URL