[leetcode] 1550. Three Consecutive Odds
原创
©著作权归作者所有:来自51CTO博客作者是念的原创作品,请联系作者获取转载授权,否则将追究法律责任
Description
Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.
Example 1:
Input: arr = [2,6,4,1]
Output: false
Explanation: There are no three consecutive odds.
Example 2:
Input: arr = [1,2,34,3,4,5,7,23,12]
Output: true
Explanation: [5,7,23] are three consecutive odds.
Constraints:
- 1 <= arr.length <= 1000.
- 1 <= arr[i] <= 1000.
分析
题目的意思是:这道题我没有搞明白为什么这么简单,思路也非常简单,O(n)的时间复杂度,直接遍历的时候统计是否有连续3次出现奇数就行了。
代码
class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
n=len(arr)
cnt=0
for i in range(n):
if(arr[i]%2==1):
cnt+=1
else:
cnt=0
if(cnt==3):
return True
return False