[leetcode] 1550. Three Consecutive Odds

网友投稿 537 2022-11-08

[leetcode] 1550. Three Consecutive Odds

[leetcode] 1550. Three Consecutive Odds

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: falseExplanation: There are no three consecutive odds.

Example 2:

Input: arr = [1,2,34,3,4,5,7,23,12]Output: trueExplanation: [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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:记一次线上SpringCloud Feign请求服务超时异常排查问题
下一篇:[leetcode] 1233. Remove Sub-Folders from the Filesystem
相关文章

 发表评论

暂时没有评论,来抢沙发吧~