题目来自于:https://leetcode-cn.com/problems/three-consecutive-odds/
给你一个整数数组 arr,请你判断数组中是否存在连续三个元素都是奇数的情况:如果存在,请返回 true ;否则,返回 false 。
Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.
示例 1:
输入:arr = [2,6,4,1]
输出:false
解释:不存在连续三个元素都是奇数的情况。
Example 1:
Input: arr = [2,6,4,1]
Output: false
Explanation: There are no three consecutive odds.
示例 2:
输入:arr = [1,2,34,3,4,5,7,23,12]
输出:true
解释:存在连续三个元素都是奇数的情况,即 [5,7,23] 。
Example 2:
Input: arr = [1,2,34,3,4,5,7,23,12]
Output: true
Explanation: [5,7,23] are three consecutive odds.
/**
* @param {number[]} arr
* @return {boolean}
*/
var threeConsecutiveOdds = function(arr) {
var num = 0;
for (let i =0;i<arr.length;i++){
if(arr[i] % 2 != 0){
num+=1;
if(num==3){
break
}
}else if (arr[i] % 2 == 0){
num=0;
}
}
return num== 3 ? true : false;
};