File tree 6 files changed +200
-2
lines changed
6 files changed +200
-2
lines changed Original file line number Diff line number Diff line change 19
19
20
20
<!-- 这里可写通用的实现逻辑 -->
21
21
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
+
22
30
<!-- tabs:start -->
23
31
24
32
### ** Python3**
25
33
26
34
<!-- 这里可写当前语言的特殊实现逻辑 -->
27
35
28
36
``` 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
30
47
```
31
48
32
49
### ** Java**
33
50
34
51
<!-- 这里可写当前语言的特殊实现逻辑 -->
35
52
36
53
``` 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
+ ```
37
89
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
+ }
38
112
```
39
113
40
114
### ** ...**
Original file line number Diff line number Diff line change 29
29
### ** Python3**
30
30
31
31
``` 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
33
42
```
34
43
35
44
### ** Java**
36
45
37
46
``` 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
+ ```
38
82
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
+ }
39
105
```
40
106
41
107
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments