题目描述
解法
设置一个变量temp
,当累计到3时跳出循环。时间复杂度
O
(
n
)
O(n)
O(n),空间复杂度
O
(
1
)
O(1)
O(1)。
Python代码:
class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
temp = 0
isFound = False
for i in range(len(arr)):
if arr[i] % 2 != 0:
temp += 1
if temp == 3:
isFound = True
break
else:
temp = 0
return isFound
C++代码:
class Solution {
public:
bool threeConsecutiveOdds(vector<int>& arr) {
int temp = 0;
bool isFound = false;
for(int i=0;i<arr.size();i++){
if(arr[i] % 2 != 0){
temp++;
if(temp == 3){
isFound = true;
break;
}
}else{
temp = 0;
}
}
return isFound;
}
};