1550. 存在连续三个奇数的数组 LeetCode第202场周赛

1550. 存在连续三个奇数的数组 LeetCode第202场周赛

传送门

传送门

结题思路

// 思路1:外层for循环遍历0-n-3个元素,内层for循环遍历是否有连续三个为奇数。
// 总结:正常双层循环遍历即可。

class Solution {
    public boolean threeConsecutiveOdds(int[] arr) {
        for(int i = 0; i < arr.length-2; i++)
        {
            int count = 0;
            for(int j = 0; j < 3; j++)
            {
                if(arr[i+j] % 2 != 0)
                {
                    ++count;
                }
            }
            if(count == 3)
            {
                return true;
            }
        }
        return false;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40092110/article/details/108118122