-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0231.java
More file actions
35 lines (31 loc) · 969 Bytes
/
_0231.java
File metadata and controls
35 lines (31 loc) · 969 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
package com.github.aditya;
public class _0231 {
// Power of Two => They will have only 1 set bit - thus taking the bitwise & operator to get the set bits.
// 1 ms, faster than 97.90%, memory 39.5 MB, less than 88.84%
class Solution {
public boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
}
// 1 ms, faster than 97.90%, memory 41 MB, less than 66.65%
class Solution_1 {
public boolean isPowerOfTwo(int n) {
int count = 0;
while (n > 0) {
n = n & (n - 1);
count++;
}
return count == 1;
}
}
// 1 ms, faster than 97.90%, memory 39.1 MB, less than 98.44%
class Solution_2 {
public boolean isPowerOfTwo(int n) {
long temp = 1;
while (temp < n) {
temp = temp << 1; // multiply by 2
}
return temp == n;
}
}
}