@@ -51,11 +51,15 @@ tags:
51
51
52
52
<!-- solution:start -->
53
53
54
- ### 方法一:遍历数组
54
+ ### 方法一:遍历 + 计数
55
55
56
- 直接遍历数组,统计连续奇数的个数,如果个数达到 3,则返回 ` true ` ,否则遍历结束,返回 ` false ` 。
56
+ 我们用一个变量 $\text{cnt}$ 记录当前连续奇数的个数 。
57
57
58
- 时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 为数组 ` arr ` 的长度。
58
+ 接下来,我们遍历数组,如果当前元素是奇数,则 $\text{cnt}$ 加一,如果 $\text{cnt}$ 等于 3,则返回 $\text{True}$。如果当前元素是偶数,则 $\text{cnt}$ 置零。
59
+
60
+ 遍历结束后,如果没有找到连续三个奇数,则返回 $\text{False}$。
61
+
62
+ 时间复杂度 $O(n)$,其中 $n$ 是数组 $\text{arr}$ 的长度。空间复杂度 $O(1)$。
59
63
60
64
<!-- tabs:start -->
61
65
@@ -65,13 +69,13 @@ tags:
65
69
class Solution :
66
70
def threeConsecutiveOdds (self , arr : List[int ]) -> bool :
67
71
cnt = 0
68
- for v in arr:
69
- if v & 1 :
72
+ for x in arr:
73
+ if x & 1 :
70
74
cnt += 1
75
+ if cnt == 3 :
76
+ return True
71
77
else :
72
78
cnt = 0
73
- if cnt == 3 :
74
- return True
75
79
return False
76
80
```
77
81
@@ -81,15 +85,14 @@ class Solution:
81
85
class Solution {
82
86
public boolean threeConsecutiveOdds (int [] arr ) {
83
87
int cnt = 0 ;
84
- for (int v : arr) {
85
- if (v % 2 == 1 ) {
86
- ++ cnt;
88
+ for (int x : arr) {
89
+ if (x % 2 == 1 ) {
90
+ if (++ cnt == 3 ) {
91
+ return true ;
92
+ }
87
93
} else {
88
94
cnt = 0 ;
89
95
}
90
- if (cnt == 3 ) {
91
- return true ;
92
- }
93
96
}
94
97
return false ;
95
98
}
@@ -103,12 +106,14 @@ class Solution {
103
106
public:
104
107
bool threeConsecutiveOdds(vector<int >& arr) {
105
108
int cnt = 0;
106
- for (int v : arr) {
107
- if (v & 1)
108
- ++cnt;
109
- else
109
+ for (int x : arr) {
110
+ if (x & 1) {
111
+ if (++cnt == 3) {
112
+ return true;
113
+ }
114
+ } else {
110
115
cnt = 0;
111
- if (cnt == 3) return true;
116
+ }
112
117
}
113
118
return false;
114
119
}
@@ -120,15 +125,15 @@ public:
120
125
```go
121
126
func threeConsecutiveOdds(arr []int) bool {
122
127
cnt := 0
123
- for _, v := range arr {
124
- if v%2 == 1 {
128
+ for _, x := range arr {
129
+ if x&1 == 1 {
125
130
cnt++
131
+ if cnt == 3 {
132
+ return true
133
+ }
126
134
} else {
127
135
cnt = 0
128
136
}
129
- if cnt == 3 {
130
- return true
131
- }
132
137
}
133
138
return false
134
139
}
@@ -139,15 +144,14 @@ func threeConsecutiveOdds(arr []int) bool {
139
144
``` ts
140
145
function threeConsecutiveOdds(arr : number []): boolean {
141
146
let cnt = 0 ;
142
- for (const v of arr ) {
143
- if (v & 1 ) {
144
- ++ cnt ;
147
+ for (const x of arr ) {
148
+ if (x & 1 ) {
149
+ if (++ cnt == 3 ) {
150
+ return true ;
151
+ }
145
152
} else {
146
153
cnt = 0 ;
147
154
}
148
- if (cnt == 3 ) {
149
- return true ;
150
- }
151
155
}
152
156
return false ;
153
157
}
@@ -159,7 +163,13 @@ function threeConsecutiveOdds(arr: number[]): boolean {
159
163
160
164
<!-- solution: start -->
161
165
162
- ### 方法二
166
+ ### 方法二:遍历 + 位运算
167
+
168
+ 根据位运算的性质,两个数进行按位与运算是奇数,当且仅当两个数都是奇数。如果有连续三个数按位与运算的结果是奇数,那么这三个数都是奇数。
169
+
170
+ 因此,我们只需要遍历数组,判断是否存在连续三个数的按位与结果是否是奇数即可,如果存在则返回 $\text{True}$,否则返回 $\text{False}$。
171
+
172
+ 时间复杂度 $O(n)$,其中 $n$ 是数组 $\text{arr}$ 的长度。空间复杂度 $O(1)$。
163
173
164
174
<!-- tabs: start -->
165
175
@@ -168,10 +178,65 @@ function threeConsecutiveOdds(arr: number[]): boolean {
168
178
``` python
169
179
class Solution :
170
180
def threeConsecutiveOdds (self , arr : List[int ]) -> bool :
171
- for i in range (len (arr) - 2 ):
172
- if arr[i] % 2 + arr[i + 1 ] % 2 + arr[i + 2 ] % 2 == 3 :
173
- return True
174
- return False
181
+ return any (x & arr[i + 1 ] & arr[i + 2 ] & 1 for i, x in enumerate (arr[:- 2 ]))
182
+ ```
183
+
184
+ #### Java
185
+
186
+ ``` java
187
+ class Solution {
188
+ public boolean threeConsecutiveOdds (int [] arr ) {
189
+ for (int i = 2 , n = arr. length; i < n; ++ i) {
190
+ if ((arr[i - 2 ] & arr[i - 1 ] & arr[i] & 1 ) == 1 ) {
191
+ return true ;
192
+ }
193
+ }
194
+ return false ;
195
+ }
196
+ }
197
+ ```
198
+
199
+ #### C++
200
+
201
+ ``` cpp
202
+ class Solution {
203
+ public:
204
+ bool threeConsecutiveOdds(vector<int >& arr) {
205
+ for (int i = 2, n = arr.size(); i < n; ++i) {
206
+ if (arr[ i - 2] & arr[ i - 1] & arr[ i] & 1) {
207
+ return true;
208
+ }
209
+ }
210
+ return false;
211
+ }
212
+ };
213
+ ```
214
+
215
+ #### Go
216
+
217
+ ```go
218
+ func threeConsecutiveOdds(arr []int) bool {
219
+ for i, n := 2, len(arr); i < n; i++ {
220
+ if arr[i-2]&arr[i-1]&arr[i]&1 == 1 {
221
+ return true
222
+ }
223
+ }
224
+ return false
225
+ }
226
+ ```
227
+
228
+ #### TypeScript
229
+
230
+ ``` ts
231
+ function threeConsecutiveOdds(arr : number []): boolean {
232
+ const n = arr .length ;
233
+ for (let i = 2 ; i < n ; ++ i ) {
234
+ if (arr [i - 2 ] & arr [i - 1 ] & arr [i ] & 1 ) {
235
+ return true ;
236
+ }
237
+ }
238
+ return false ;
239
+ }
175
240
```
176
241
177
242
<!-- tabs: end -->
0 commit comments