공부/프로그래머스

짝수는 싫어요

딸기버블티 2024. 1. 25. 15:44

 

class Solution {
    public int[] solution(int n) {
        int length = n % 2 == 0 ? n/2 : n/2 + 1;
        int[] answer = new int[length];
        int index = 0;
        for(int i=0; i<=n; i++){
            if(i % 2 != 0){
                answer[index] = i;
                index++;
            }
        }
        return answer;
    }
}