[Programmers] 코딩테스트 입문 120824. 짝수 홀수 개수

이지현·2023년 2월 1일
0

Algorithm

목록 보기
13/81
post-thumbnail

✔️ Problem URL

짝수 홀수 개수


✔️ Problem

정수가 담긴 리스트 num_list가 주어질 때, num_list의 원소 중 짝수와 홀수의 개수를 담은 배열을 return 하도록 solution 함수를 완성해보세요.


✔️ Code

class Solution {
    public int[] solution(int[] num_list) {
        int[] answer = new int[2];
        int cnt1 = 0;
        int cnt2 = 0;
        
        for(int i = 0; i < num_list.length; i++) {
            if(num_list[i] % 2 == 0) {
                cnt1++;
                answer[0] = cnt1;
            }
            else if(num_list[i] % 2 == 1) {
                cnt2++;
                answer[1] = cnt2;
            }
        }
        return answer;
    }
}
profile
2023.09 ~ 티스토리 이전 / 2024.04 ~ 깃허브 블로그 이전

0개의 댓글