存在连续三个奇数的数组
Given an integer array arr, return true if there are three consecutive (adj. 连贯的;连续不断的)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.
class Solution {
public boolean threeConsecutiveOdds(int[] arr) {
int cnt=0;
boolean flag=false;
for(int i=0;i<arr.length;i++){
if(arr[i]%2==1)
cnt++;
if(arr[i]%2!=1)
cnt=0;
if(cnt==3)
flag=true;
}
return flag?true:false;
}
}
循环如果是奇数cnt++,但是只要碰到不是奇数的cnt就=0重新计算,
如果cnt==3满足条件设flag=true.循环结束后,判断有没有
flag=true,有的话那就有连续的3个奇数,否则就没有