LeetCode 1550 Three Consecutive Odds Writeup
作者:互联网
存在连续三个奇数的数组
解法一
class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
pos = 0
while pos + 2 < len(arr):
if arr[pos] & 1 == 1 and arr[pos + 1] & 1 == 1 and arr[pos + 2] & 1 == 1:
return True
pos += 1
return False
解法二
class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
pos = 0
count = 0
while pos <len(arr):
if arr[pos] & 1 == 1:
count += 1
else:
count = 0
if count == 3:
return True
pos += 1
return False
复杂度分析
- 空间复杂度:两种算法的复杂度都为O(n),后一种要稍好一些
- 空间复杂度:都为O(1)
标签:1550,arr,int,Writeup,Odds,pos,self,threeConsecutiveOdds,复杂度 来源: https://blog.csdn.net/qq_41529090/article/details/113325119