|
| 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> </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> </p> |
| 31 | + |
| 32 | +<p><strong>提示:</strong></p> |
| 33 | + |
| 34 | +<ul> |
| 35 | + <li><code>1 <= arr.length <= 1000</code></li> |
| 36 | + <li><code>1 <= arr[i] <= 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) |
0 commit comments