[LeetCode] Three Consecutive Odds

아르당·2026년 5월 24일

LeetCode

목록 보기
325/602
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

정수 배열 arr이 주어졌을 때, 배열에 연속된 홀수가 3개 있으면 true를 반환해라. 그렇지 않다면 false를 반환해라.

Example

#1
Input: arr = [2, 6, 4, 1]
Output: false
Explanation: 연속된 홀수 3개가 없다.

#2
Input: arr = [1, 2, 34, 3, 4, 5, 7, 23, 12]
Output: true
Explanation: [5, 7, 23]은 연속된 홀수 3개이다.

Constraints

  • 1 <= arr.length <= 1000
  • 1 <= arr[i] <= 1000

Solved

class Solution {
    public boolean threeConsecutiveOdds(int[] arr) {
        int count = 0;

        for(int i : arr){
            if(i % 2 == 1){
                count++;
            }else{
                count = 0;
            }

            if(count == 3){
                return true;
            }
        }

        return false;
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글