포스트

짝수 홀수 개수

https://school.programmers.co.kr/learn/courses/30/lessons/120824

문제

  • 문제 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
    public int[] solution(int[] num_list) {
        int evenCount = 0;
        int oddCount = 0;
        
        for (int num : num_list) {
            if (num % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
        }
        
        return new int[]{evenCount, oddCount};
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
    public int[] solution(int[] num_list) {
        int[] count = new int[2];

        for (int i : num_list) {
            if (i % 2 == 0) {
                count[0]++;
            } else {
                count[1]++;
            }
        }
        return count;
    }
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.