Skip to content

Commit cbb6a0e

Browse files
authored
Update README_EN.md
1 parent 0fdfd6d commit cbb6a0e

File tree

1 file changed

+88
-35
lines changed

1 file changed

+88
-35
lines changed

solution/1500-1599/1550.Three Consecutive Odds/README_EN.md

Lines changed: 88 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ Given an integer array <code>arr</code>, return <code>true</code>&nbsp;if there
5151

5252
<!-- solution:start -->
5353

54-
### Solution 1
54+
### Solution 1: Iteration + Counting
55+
56+
We use a variable $\text{cnt}$ to record the current count of consecutive odd numbers.
57+
58+
Next, we iterate through the array. If the current element is odd, then $\text{cnt}$ is incremented by one. If $\text{cnt}$ equals 3, then return $\text{True}$. If the current element is even, then $\text{cnt}$ is reset to zero.
59+
60+
After the iteration, if three consecutive odd numbers are not found, then return $\text{False}$.
61+
62+
The time complexity is $O(n)$, where $n$ is the length of the array $\text{arr}$. The space complexity is $O(1)$.
5563

5664
<!-- tabs:start -->
5765

@@ -61,13 +69,13 @@ Given an integer array <code>arr</code>, return <code>true</code>&nbsp;if there
6169
class Solution:
6270
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
6371
cnt = 0
64-
for v in arr:
65-
if v & 1:
72+
for x in arr:
73+
if x & 1:
6674
cnt += 1
75+
if cnt == 3:
76+
return True
6777
else:
6878
cnt = 0
69-
if cnt == 3:
70-
return True
7179
return False
7280
```
7381

@@ -77,15 +85,14 @@ class Solution:
7785
class Solution {
7886
public boolean threeConsecutiveOdds(int[] arr) {
7987
int cnt = 0;
80-
for (int v : arr) {
81-
if (v % 2 == 1) {
82-
++cnt;
88+
for (int x : arr) {
89+
if (x % 2 == 1) {
90+
if (++cnt == 3) {
91+
return true;
92+
}
8393
} else {
8494
cnt = 0;
8595
}
86-
if (cnt == 3) {
87-
return true;
88-
}
8996
}
9097
return false;
9198
}
@@ -99,12 +106,14 @@ class Solution {
99106
public:
100107
bool threeConsecutiveOdds(vector<int>& arr) {
101108
int cnt = 0;
102-
for (int v : arr) {
103-
if (v & 1)
104-
++cnt;
105-
else
109+
for (int x : arr) {
110+
if (x & 1) {
111+
if (++cnt == 3) {
112+
return true;
113+
}
114+
} else {
106115
cnt = 0;
107-
if (cnt == 3) return true;
116+
}
108117
}
109118
return false;
110119
}
@@ -116,15 +125,15 @@ public:
116125
```go
117126
func threeConsecutiveOdds(arr []int) bool {
118127
cnt := 0
119-
for _, v := range arr {
120-
if v%2 == 1 {
128+
for _, x := range arr {
129+
if x&1 == 1 {
121130
cnt++
131+
if cnt == 3 {
132+
return true
133+
}
122134
} else {
123135
cnt = 0
124136
}
125-
if cnt == 3 {
126-
return true
127-
}
128137
}
129138
return false
130139
}
@@ -135,15 +144,14 @@ func threeConsecutiveOdds(arr []int) bool {
135144
```ts
136145
function threeConsecutiveOdds(arr: number[]): boolean {
137146
let cnt = 0;
138-
for (const v of arr) {
139-
if (v & 1) {
140-
++cnt;
147+
for (const x of arr) {
148+
if (x & 1) {
149+
if (++cnt == 3) {
150+
return true;
151+
}
141152
} else {
142153
cnt = 0;
143154
}
144-
if (cnt == 3) {
145-
return true;
146-
}
147155
}
148156
return false;
149157
}
@@ -155,7 +163,13 @@ function threeConsecutiveOdds(arr: number[]): boolean {
155163

156164
<!-- solution:start -->
157165

158-
### Solution 2
166+
### Solution 2: Iteration + Bitwise Operation
167+
168+
Based on the properties of bitwise operations, the result of a bitwise AND operation between two numbers is odd if and only if both numbers are odd. If there are three consecutive numbers whose bitwise AND result is odd, then these three numbers are all odd.
169+
170+
Therefore, we only need to iterate through the array and check if there exists three consecutive numbers whose bitwise AND result is odd. If such numbers exist, return $\text{True}$; otherwise, return $\text{False}$.
171+
172+
The time complexity is $O(n)$, where $n$ is the length of the array $\text{arr}$. The space complexity is $O(1)$.
159173

160174
<!-- tabs:start -->
161175

@@ -164,20 +178,59 @@ function threeConsecutiveOdds(arr: number[]): boolean {
164178
```python
165179
class Solution:
166180
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
167-
for i in range(len(arr) - 2):
168-
if arr[i] % 2 + arr[i + 1] % 2 + arr[i + 2] % 2 == 3:
169-
return True
170-
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+
}
171226
```
172227

173228
#### TypeScript
174229

175230
```ts
176231
function threeConsecutiveOdds(arr: number[]): boolean {
177232
const n = arr.length;
178-
if (n < 3) return false;
179-
180-
for (let i = 2; i < n; i++) {
233+
for (let i = 2; i < n; ++i) {
181234
if (arr[i - 2] & arr[i - 1] & arr[i] & 1) {
182235
return true;
183236
}

0 commit comments

Comments
 (0)