1550.| Three Consecutive Odds(数组)

存在连续三个奇数的数组

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个奇数,否则就没有

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值