Skip to content

Commit ae17a6a

Browse files
committedMar 28, 2022
feat: add solutions to lc problem: No.0693
No.0693.Binary Number with Alternating Bits
1 parent 3ed0ed8 commit ae17a6a

File tree

5 files changed

+170
-18
lines changed

5 files changed

+170
-18
lines changed
 

‎solution/0600-0699/0693.Binary Number with Alternating Bits/README.md

+82-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47-
假设 01 交替出现,那么我们可以通过错位异或将尾部全部转为 1,加 1 可以得到 2 的幂次的一个数 n(n 中只有一个位是 1),接着利用 `n & (n - 1)` 可以消除最后一位的 1。
47+
**方法一:模拟**
48+
49+
n 循环右移直至为 0,依次检测 n 的二进制位是否交替出现。若循环过程中发现 0、1 没有交替出现,直接返回 false。否则循环结束返回 true。
50+
51+
**方法二:位运算**
52+
53+
假设 01 交替出现,那么我们可以通过错位异或将尾部全部转为 1,加 1 可以得到 2 的幂次的一个数 n(n 中只有一个位是 1),接着利用 `n & (n + 1)` 可以消除最后一位的 1。
4854

4955
此时判断是否为 0,若是,说明假设成立,是 01 交替串。
5056

@@ -57,8 +63,21 @@
5763
```python
5864
class Solution:
5965
def hasAlternatingBits(self, n: int) -> bool:
60-
n = (n ^ (n >> 1)) + 1
61-
return (n & (n - 1)) == 0
66+
prev = -1
67+
while n:
68+
curr = n & 1
69+
if prev == curr:
70+
return False
71+
prev = curr
72+
n >>= 1
73+
return True
74+
```
75+
76+
```python
77+
class Solution:
78+
def hasAlternatingBits(self, n: int) -> bool:
79+
n ^= (n >> 1)
80+
return (n & (n + 1)) == 0
6281
```
6382

6483
### **Java**
@@ -68,14 +87,48 @@ class Solution:
6887
```java
6988
class Solution {
7089
public boolean hasAlternatingBits(int n) {
71-
n = (n ^ (n >> 1)) + 1;
72-
return (n & (n - 1)) == 0;
90+
int prev = -1;
91+
while (n != 0) {
92+
int curr = n & 1;
93+
if (prev == curr) {
94+
return false;
95+
}
96+
prev = curr;
97+
n >>= 1;
98+
}
99+
return true;
100+
}
101+
}
102+
```
103+
104+
```java
105+
class Solution {
106+
public boolean hasAlternatingBits(int n) {
107+
n ^= (n >> 1);
108+
return (n & (n + 1)) == 0;
73109
}
74110
}
75111
```
76112

77113
### **C++**
78114

115+
```cpp
116+
class Solution {
117+
public:
118+
bool hasAlternatingBits(int n) {
119+
int prev = -1;
120+
while (n)
121+
{
122+
int curr = n & 1;
123+
if (prev == curr) return false;
124+
prev = curr;
125+
n >>= 1;
126+
}
127+
return true;
128+
}
129+
};
130+
```
131+
79132
```cpp
80133
class Solution {
81134
public:
@@ -115,6 +168,30 @@ impl Solution {
115168
}
116169
```
117170

171+
### **Go**
172+
173+
```go
174+
func hasAlternatingBits(n int) bool {
175+
prev := -1
176+
for n != 0 {
177+
curr := n & 1
178+
if prev == curr {
179+
return false
180+
}
181+
prev = curr
182+
n >>= 1
183+
}
184+
return true
185+
}
186+
```
187+
188+
```go
189+
func hasAlternatingBits(n int) bool {
190+
n ^= (n >> 1)
191+
return (n & (n + 1)) == 0
192+
}
193+
```
194+
118195
### **...**
119196

120197
```

‎solution/0600-0699/0693.Binary Number with Alternating Bits/README_EN.md

+75-4
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,70 @@
4545
```python
4646
class Solution:
4747
def hasAlternatingBits(self, n: int) -> bool:
48-
n = (n ^ (n >> 1)) + 1
49-
return (n & (n - 1)) == 0
48+
prev = -1
49+
while n:
50+
curr = n & 1
51+
if prev == curr:
52+
return False
53+
prev = curr
54+
n >>= 1
55+
return True
56+
```
57+
58+
```python
59+
class Solution:
60+
def hasAlternatingBits(self, n: int) -> bool:
61+
n ^= (n >> 1)
62+
return (n & (n + 1)) == 0
5063
```
5164

5265
### **Java**
5366

5467
```java
5568
class Solution {
5669
public boolean hasAlternatingBits(int n) {
57-
n = (n ^ (n >> 1)) + 1;
58-
return (n & (n - 1)) == 0;
70+
int prev = -1;
71+
while (n != 0) {
72+
int curr = n & 1;
73+
if (prev == curr) {
74+
return false;
75+
}
76+
prev = curr;
77+
n >>= 1;
78+
}
79+
return true;
80+
}
81+
}
82+
```
83+
84+
```java
85+
class Solution {
86+
public boolean hasAlternatingBits(int n) {
87+
n ^= (n >> 1);
88+
return (n & (n + 1)) == 0;
5989
}
6090
}
6191
```
6292

6393
### **C++**
6494

95+
```cpp
96+
class Solution {
97+
public:
98+
bool hasAlternatingBits(int n) {
99+
int prev = -1;
100+
while (n)
101+
{
102+
int curr = n & 1;
103+
if (prev == curr) return false;
104+
prev = curr;
105+
n >>= 1;
106+
}
107+
return true;
108+
}
109+
};
110+
```
111+
65112
```cpp
66113
class Solution {
67114
public:
@@ -101,6 +148,30 @@ impl Solution {
101148
}
102149
```
103150

151+
### **Go**
152+
153+
```go
154+
func hasAlternatingBits(n int) bool {
155+
prev := -1
156+
for n != 0 {
157+
curr := n & 1
158+
if prev == curr {
159+
return false
160+
}
161+
prev = curr
162+
n >>= 1
163+
}
164+
return true
165+
}
166+
```
167+
168+
```go
169+
func hasAlternatingBits(n int) bool {
170+
n ^= (n >> 1)
171+
return (n & (n + 1)) == 0
172+
}
173+
```
174+
104175
### **...**
105176

106177
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
func hasAlternatingBits(n int) bool {
2+
n ^= (n >> 1)
3+
return (n & (n + 1)) == 0
4+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
class Solution {
2-
public boolean hasAlternatingBits(int n) {
3-
n = (n ^ (n >> 1)) + 1;
4-
return (n & (n - 1)) == 0;
5-
}
1+
class Solution {
2+
public boolean hasAlternatingBits(int n) {
3+
n ^= (n >> 1);
4+
return (n & (n + 1)) == 0;
5+
}
66
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Solution:
2-
def hasAlternatingBits(self, n: int) -> bool:
3-
n = (n ^ (n >> 1)) + 1
4-
return (n & (n - 1)) == 0
1+
class Solution:
2+
def hasAlternatingBits(self, n: int) -> bool:
3+
n ^= (n >> 1)
4+
return (n & (n + 1)) == 0

0 commit comments

Comments
 (0)