Skip to content

Commit 7c69318

Browse files
committed
feat: add solutions to lcci problem: No.05.03
No.05.03.Reverse Bits
1 parent fa8187d commit 7c69318

File tree

6 files changed

+200
-2
lines changed

6 files changed

+200
-2
lines changed

lcci/05.03.Reverse Bits/README.md

+75-1
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,96 @@
1919

2020
<!-- 这里可写通用的实现逻辑 -->
2121

22+
**方法一:双指针**
23+
24+
我们可以使用双指针 $i$ 和 $j$ 维护一个滑动窗口,其中 $i$ 为右指针,$j$ 为左指针。每次右指针 $i$ 向右移动一位,如果此时窗口内的 $0$ 的个数超过 $1$ 个,则左指针 $j$ 向右移动一位,直到窗口内的 $0$ 的个数不超过 $1$ 个为止。然后计算此时窗口的长度,与当前最大长度进行比较,取较大值作为当前最大长度。
25+
26+
最后返回最大长度即可。
27+
28+
时间复杂度 $O(\log M)$,空间复杂度 $O(1)$。其中 $M$ 为 $32$ 位整数的最大值。
29+
2230
<!-- tabs:start -->
2331

2432
### **Python3**
2533

2634
<!-- 这里可写当前语言的特殊实现逻辑 -->
2735

2836
```python
29-
37+
class Solution:
38+
def reverseBits(self, num: int) -> int:
39+
ans = cnt = j = 0
40+
for i in range(32):
41+
cnt += num >> i & 1 ^ 1
42+
while cnt > 1:
43+
cnt -= num >> j & 1 ^ 1
44+
j += 1
45+
ans = max(ans, i - j + 1)
46+
return ans
3047
```
3148

3249
### **Java**
3350

3451
<!-- 这里可写当前语言的特殊实现逻辑 -->
3552

3653
```java
54+
class Solution {
55+
public int reverseBits(int num) {
56+
int ans = 0, cnt = 0;
57+
for (int i = 0, j = 0; i < 32; ++i) {
58+
cnt += num >> i & 1 ^ 1;
59+
while (cnt > 1) {
60+
cnt -= num >> j & 1 ^ 1;
61+
++j;
62+
}
63+
ans = Math.max(ans, i - j + 1);
64+
}
65+
return ans;
66+
}
67+
}
68+
```
69+
70+
### **C++**
71+
72+
```cpp
73+
class Solution {
74+
public:
75+
int reverseBits(int num) {
76+
int ans = 0, cnt = 0;
77+
for (int i = 0, j = 0; i < 32; ++i) {
78+
cnt += num >> i & 1 ^ 1;
79+
while (cnt > 1) {
80+
cnt -= num >> j & 1 ^ 1;
81+
++j;
82+
}
83+
ans = max(ans, i - j + 1);
84+
}
85+
return ans;
86+
}
87+
};
88+
```
3789
90+
### **Go**
91+
92+
```go
93+
func reverseBits(num int) (ans int) {
94+
var cnt, j int
95+
for i := 0; i < 32; i++ {
96+
cnt += num>>i&1 ^ 1
97+
for cnt > 1 {
98+
cnt -= num>>j&1 ^ 1
99+
j++
100+
}
101+
ans = max(ans, i-j+1)
102+
}
103+
return
104+
}
105+
106+
func max(a, b int) int {
107+
if a > b {
108+
return a
109+
}
110+
return b
111+
}
38112
```
39113

40114
### **...**

lcci/05.03.Reverse Bits/README_EN.md

+67-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,79 @@
2929
### **Python3**
3030

3131
```python
32-
32+
class Solution:
33+
def reverseBits(self, num: int) -> int:
34+
ans = cnt = j = 0
35+
for i in range(32):
36+
cnt += num >> i & 1 ^ 1
37+
while cnt > 1:
38+
cnt -= num >> j & 1 ^ 1
39+
j += 1
40+
ans = max(ans, i - j + 1)
41+
return ans
3342
```
3443

3544
### **Java**
3645

3746
```java
47+
class Solution {
48+
public int reverseBits(int num) {
49+
int ans = 0, cnt = 0;
50+
for (int i = 0, j = 0; i < 32; ++i) {
51+
cnt += num >> i & 1 ^ 1;
52+
while (cnt > 1) {
53+
cnt -= num >> j & 1 ^ 1;
54+
++j;
55+
}
56+
ans = Math.max(ans, i - j + 1);
57+
}
58+
return ans;
59+
}
60+
}
61+
```
62+
63+
### **C++**
64+
65+
```cpp
66+
class Solution {
67+
public:
68+
int reverseBits(int num) {
69+
int ans = 0, cnt = 0;
70+
for (int i = 0, j = 0; i < 32; ++i) {
71+
cnt += num >> i & 1 ^ 1;
72+
while (cnt > 1) {
73+
cnt -= num >> j & 1 ^ 1;
74+
++j;
75+
}
76+
ans = max(ans, i - j + 1);
77+
}
78+
return ans;
79+
}
80+
};
81+
```
3882
83+
### **Go**
84+
85+
```go
86+
func reverseBits(num int) (ans int) {
87+
var cnt, j int
88+
for i := 0; i < 32; i++ {
89+
cnt += num>>i&1 ^ 1
90+
for cnt > 1 {
91+
cnt -= num>>j&1 ^ 1
92+
j++
93+
}
94+
ans = max(ans, i-j+1)
95+
}
96+
return
97+
}
98+
99+
func max(a, b int) int {
100+
if a > b {
101+
return a
102+
}
103+
return b
104+
}
39105
```
40106

41107
### **...**

lcci/05.03.Reverse Bits/Solution.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int reverseBits(int num) {
4+
int ans = 0, cnt = 0;
5+
for (int i = 0, j = 0; i < 32; ++i) {
6+
cnt += num >> i & 1 ^ 1;
7+
while (cnt > 1) {
8+
cnt -= num >> j & 1 ^ 1;
9+
++j;
10+
}
11+
ans = max(ans, i - j + 1);
12+
}
13+
return ans;
14+
}
15+
};

lcci/05.03.Reverse Bits/Solution.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func reverseBits(num int) (ans int) {
2+
var cnt, j int
3+
for i := 0; i < 32; i++ {
4+
cnt += num>>i&1 ^ 1
5+
for cnt > 1 {
6+
cnt -= num>>j&1 ^ 1
7+
j++
8+
}
9+
ans = max(ans, i-j+1)
10+
}
11+
return
12+
}
13+
14+
func max(a, b int) int {
15+
if a > b {
16+
return a
17+
}
18+
return b
19+
}

lcci/05.03.Reverse Bits/Solution.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int reverseBits(int num) {
3+
int ans = 0, cnt = 0;
4+
for (int i = 0, j = 0; i < 32; ++i) {
5+
cnt += num >> i & 1 ^ 1;
6+
while (cnt > 1) {
7+
cnt -= num >> j & 1 ^ 1;
8+
++j;
9+
}
10+
ans = Math.max(ans, i - j + 1);
11+
}
12+
return ans;
13+
}
14+
}

lcci/05.03.Reverse Bits/Solution.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def reverseBits(self, num: int) -> int:
3+
ans = cnt = j = 0
4+
for i in range(32):
5+
cnt += num >> i & 1 ^ 1
6+
while cnt > 1:
7+
cnt -= num >> j & 1 ^ 1
8+
j += 1
9+
ans = max(ans, i - j + 1)
10+
return ans

0 commit comments

Comments
 (0)