Skip to content

Commit cb83cd5

Browse files
LetMeFly666Copilot
andauthored
update: 添加问题“1550.存在连续三个奇数的数组”的代码和题解(#930)
* 2918: fix.typo (#974) https://github.com/LetMeFly666/LeetCode/pull/928\#discussion_r2083121877 Signed-off-by: LetMeFly666 <814114971@qq.com> * MGJW: 周报 (#929) Signed-off-by: LetMeFly666 <814114971@qq.com> * words: en (#929) Signed-off-by: LetMeFly666 <814114971@qq.com> * 1550: RE.C++ (#929) Signed-off-by: LetMeFly666 <814114971@qq.com> * 1550: AC.C++ - AC,100.00%,30.89% (#929) Signed-off-by: LetMeFly666 <814114971@qq.com> * 1550: WA.Py (#929) Signed-off-by: LetMeFly666 <814114971@qq.com> * 1550: AC.Py - AC,100.00%,93.48% (#929) Signed-off-by: LetMeFly666 <814114971@qq.com> * 1550: AC.Java - AC,100.00%,88.19% (#929) Signed-off-by: LetMeFly666 <814114971@qq.com> * 1550: CE.go (#929) Signed-off-by: LetMeFly666 <814114971@qq.com> * 1550: AC.go - AC,100.00%,25.00% (#929) Signed-off-by: LetMeFly666 <814114971@qq.com> * update: 添加问题“1550.存在连续三个奇数的数组”的代码和题解(#930) Signed-off-by: LetMeFly666 <814114971@qq.com> * Update Solutions/Other-English-LearningNotes-SomeWords.md by accepting colpilot's questions Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * MGJW: 开会 Signed-off-by: LetMeFly666 <814114971@qq.com> --------- Signed-off-by: LetMeFly666 <814114971@qq.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 191074f commit cb83cd5

11 files changed

+1258
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-11 14:00:52
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-11 14:13:46
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
bool threeConsecutiveOdds(vector<int>& arr) {
14+
for (int i = 2; i < arr.size(); i++) {
15+
if (arr[i] % 2 && arr[i - 1] % 2 && arr[i - 2] % 2) {
16+
return true;
17+
}
18+
}
19+
return false;
20+
}
21+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-11 14:00:52
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-11 14:19:31
6+
*/
7+
package main
8+
9+
func threeConsecutiveOdds(arr []int) bool {
10+
for i := 2; i < len(arr); i++ {
11+
if arr[i] % 2 == 1 && arr[i - 1] % 2 == 1 && arr[i - 2] % 2 == 1 {
12+
return true
13+
}
14+
}
15+
return false;
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-11 14:00:52
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-11 14:17:39
6+
* @Description: 1550: AC,100.00%,88.19%
7+
*/
8+
class Solution {
9+
public boolean threeConsecutiveOdds(int[] arr) {
10+
for (int i = 2; i < arr.length; i++) {
11+
if (arr[i] % 2 == 1 && arr[i - 1] % 2 == 1 && arr[i - 2] % 2 == 1) {
12+
return true;
13+
}
14+
}
15+
return false;
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-05-11 14:00:52
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-05-11 14:16:01
6+
Description: AC,100.00%,93.48%
7+
'''
8+
from typing import List
9+
10+
class Solution:
11+
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
12+
for i in range(2, len(arr)):
13+
if arr[i] % 2 and arr[i - 1] % 2 and arr[i - 2] % 2:
14+
return True
15+
return False

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@
552552
|1534.统计好三元组|简单|<a href="https://leetcode.cn/problems/count-good-triplets/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/15/LeetCode%201534.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E4%B8%89%E5%85%83%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147249275" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-good-triplets/solutions/3651795/letmefly-1534tong-ji-hao-san-yuan-zu-xi-dv8dw/" target="_blank">LeetCode题解</a>|
553553
|1535.找出数组游戏的赢家|中等|<a href="https://leetcode.cn/problems/find-the-winner-of-an-array-game/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/19/LeetCode%201535.%E6%89%BE%E5%87%BA%E6%95%B0%E7%BB%84%E6%B8%B8%E6%88%8F%E7%9A%84%E8%B5%A2%E5%AE%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139040126" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-winner-of-an-array-game/solutions/2782688/letmefly-1535zhao-chu-shu-zu-you-xi-de-y-ff3w/" target="_blank">LeetCode题解</a>|
554554
|1542.找出最长的超赞子字符串|困难|<a href="https://leetcode.cn/problems/find-longest-awesome-substring/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/20/LeetCode%201542.%E6%89%BE%E5%87%BA%E6%9C%80%E9%95%BF%E7%9A%84%E8%B6%85%E8%B5%9E%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139077950" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-longest-awesome-substring/solutions/2784697/letmefly-1542zhao-chu-zui-chang-de-chao-16nco/" target="_blank">LeetCode题解</a>|
555+
|1550.存在连续三个奇数的数组|简单|<a href="https://leetcode.cn/problems/three-consecutive-odds/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/11/LeetCode%201550.%E5%AD%98%E5%9C%A8%E8%BF%9E%E7%BB%AD%E4%B8%89%E4%B8%AA%E5%A5%87%E6%95%B0%E7%9A%84%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147872651" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/three-consecutive-odds/solutions/3674069/letmefly-1550cun-zai-lian-xu-san-ge-qi-s-1mbl/" target="_blank">LeetCode题解</a>|
555556
|1552.两球之间的磁力|中等|<a href="https://leetcode.cn/problems/magnetic-force-between-two-balls/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/14/LeetCode%201552.%E4%B8%A4%E7%90%83%E4%B9%8B%E9%97%B4%E7%9A%84%E7%A3%81%E5%8A%9B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145629037" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/magnetic-force-between-two-balls/solutions/3074287/letmefly-1552liang-qiu-zhi-jian-de-ci-li-ptbu/" target="_blank">LeetCode题解</a>|
556557
|1572.矩阵对角线元素的和|简单|<a href="https://leetcode.cn/problems/matrix-diagonal-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/08/11/LeetCode%201572.%E7%9F%A9%E9%98%B5%E5%AF%B9%E8%A7%92%E7%BA%BF%E5%85%83%E7%B4%A0%E7%9A%84%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132223172" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/matrix-diagonal-sum/solutions/2382760/letmefly-1572ju-zhen-dui-jiao-xian-yuan-5qczr/" target="_blank">LeetCode题解</a>|
557558
|1574.删除最短的子数组使剩余数组有序|中等|<a href="https://leetcode.cn/problems/shortest-subarray-to-be-removed-to-make-array-sorted/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/25/LeetCode%201574.%E5%88%A0%E9%99%A4%E6%9C%80%E7%9F%AD%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E4%BD%BF%E5%89%A9%E4%BD%99%E6%95%B0%E7%BB%84%E6%9C%89%E5%BA%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129763510" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/shortest-subarray-to-be-removed-to-make-array-sorted/solutions/2189467/letmefly-1574shan-chu-zui-duan-de-zi-shu-inda/" target="_blank">LeetCode题解</a>|
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
title: 1550.存在连续三个奇数的数组:遍历
3+
date: 2025-05-11 14:25:21
4+
tags: [题解, LeetCode, 简单, 数组, 遍历]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】1550.存在连续三个奇数的数组:遍历
9+
10+
力扣题目链接:[https://leetcode.cn/problems/three-consecutive-odds/](https://leetcode.cn/problems/three-consecutive-odds/)
11+
12+
<p>给你一个整数数组 <code>arr</code>,请你判断数组中是否存在连续三个元素都是奇数的情况:如果存在,请返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
13+
14+
<p>&nbsp;</p>
15+
16+
<p><strong>示例 1:</strong></p>
17+
18+
<pre><strong>输入:</strong>arr = [2,6,4,1]
19+
<strong>输出:</strong>false
20+
<strong>解释:</strong>不存在连续三个元素都是奇数的情况。
21+
</pre>
22+
23+
<p><strong>示例 2:</strong></p>
24+
25+
<pre><strong>输入:</strong>arr = [1,2,34,3,4,5,7,23,12]
26+
<strong>输出:</strong>true
27+
<strong>解释:</strong>存在连续三个元素都是奇数的情况,即 [5,7,23] 。
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
32+
<p><strong>提示:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= arr.length &lt;= 1000</code></li>
36+
<li><code>1 &lt;= arr[i] &lt;= 1000</code></li>
37+
</ul>
38+
39+
40+
41+
## 解题方法:遍历
42+
43+
从第3个元素(下标2)开始向后遍历,若遇到连续3个奇数则直接返回true,否则返回false。
44+
45+
+ 时间复杂度$O(len(nums))$
46+
+ 空间复杂度$O(1)$
47+
48+
### AC代码
49+
50+
#### C++
51+
52+
```cpp
53+
/*
54+
* @Author: LetMeFly
55+
* @Date: 2025-05-11 14:00:52
56+
* @LastEditors: LetMeFly.xyz
57+
* @LastEditTime: 2025-05-11 14:13:46
58+
*/
59+
class Solution {
60+
public:
61+
bool threeConsecutiveOdds(vector<int>& arr) {
62+
for (int i = 2; i < arr.size(); i++) {
63+
if (arr[i] % 2 && arr[i - 1] % 2 && arr[i - 2] % 2) {
64+
return true;
65+
}
66+
}
67+
return false;
68+
}
69+
};
70+
```
71+
72+
#### Python
73+
74+
```python
75+
'''
76+
Author: LetMeFly
77+
Date: 2025-05-11 14:00:52
78+
LastEditors: LetMeFly.xyz
79+
LastEditTime: 2025-05-11 14:16:01
80+
Description: AC,100.00%,93.48%
81+
'''
82+
from typing import List
83+
84+
class Solution:
85+
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
86+
for i in range(2, len(arr)):
87+
if arr[i] % 2 and arr[i - 1] % 2 and arr[i - 2] % 2:
88+
return True
89+
return False
90+
```
91+
92+
#### Java
93+
94+
```java
95+
/*
96+
* @Author: LetMeFly
97+
* @Date: 2025-05-11 14:00:52
98+
* @LastEditors: LetMeFly.xyz
99+
* @LastEditTime: 2025-05-11 14:17:39
100+
* @Description: 1550: AC,100.00%,88.19%
101+
*/
102+
class Solution {
103+
public boolean threeConsecutiveOdds(int[] arr) {
104+
for (int i = 2; i < arr.length; i++) {
105+
if (arr[i] % 2 == 1 && arr[i - 1] % 2 == 1 && arr[i - 2] % 2 == 1) {
106+
return true;
107+
}
108+
}
109+
return false;
110+
}
111+
}
112+
```
113+
114+
#### Go
115+
116+
```go
117+
/*
118+
* @Author: LetMeFly
119+
* @Date: 2025-05-11 14:00:52
120+
* @LastEditors: LetMeFly.xyz
121+
* @LastEditTime: 2025-05-11 14:19:31
122+
*/
123+
package main
124+
125+
func threeConsecutiveOdds(arr []int) bool {
126+
for i := 2; i < len(arr); i++ {
127+
if arr[i] % 2 == 1 && arr[i - 1] % 2 == 1 && arr[i - 2] % 2 == 1 {
128+
return true
129+
}
130+
}
131+
return false;
132+
}
133+
```
134+
135+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147872651)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/05/11/LeetCode%201550.%E5%AD%98%E5%9C%A8%E8%BF%9E%E7%BB%AD%E4%B8%89%E4%B8%AA%E5%A5%87%E6%95%B0%E7%9A%84%E6%95%B0%E7%BB%84/)哦~
136+
>
137+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/LeetCode 2918.数组的最小相等和.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ categories: [题解, LeetCode]
5050
假设$nums1$的和为$s1$且$nums1$中有$c1$个$0$,$nums2$的和以及零的个数分别为$s2$和$c2$,则有:
5151

5252
+ 若$s1 < s2 + c2 and c1 == 0$,说明$nums2$的和至少变为$s2+c2$,比$s1$大并且$s1$中没有$0$可以使其和变大,直接返回$-1$;
53-
+ 若$s1 + c1 > s2 and cs == 0$,同理,直接返回$-1$;
53+
+ 若$s1 + c1 > s2 and c2 == 0$,同理,直接返回$-1$;
5454
+ 否则,返回$max(s1 + c1, s2 + c2)$。
5555

5656
时空复杂度分析:

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,8 @@ categories: [自用]
11421142
|deficient|adj. 缺乏的,不足的,有缺点的,有缺陷的|
11431143
|||
11441144
|barometer|n. 气压计,晴雨表|
1145+
|||
1146+
|earnest|adj. 非常认真的,诚实的,真诚的<br/>n. 认真,热心,保证金,预兆|
11451147

11461148
<p class="wordCounts">单词收录总数</p>
11471149

0 commit comments

Comments
 (0)