-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path693.java
More file actions
30 lines (30 loc) · 935 Bytes
/
693.java
File metadata and controls
30 lines (30 loc) · 935 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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public boolean hasAlternatingBits(int n) {
int cur = n % 2; // Rightest bit
n /= 2;
while (n > 0) {
if (cur == n % 2) return false;
cur = n % 2;
n /= 2;
}
return true;
}
}
__________________________________________________________________________________________________
sample 31820 kb submission
class Solution {
public boolean hasAlternatingBits(int n) {
if (n < 2) return true;
int last = n % 2;
while (n >= 1){
n >>= 1;
int tmp = (n%2) & 1;
if (tmp == last)return false;
last = tmp;
}
return true;
}
}
__________________________________________________________________________________________________