본문 바로가기
(문제풀이)

프로그래머스 (java) : 짝수는 싫어요

by cogito-new 2022. 10. 10.

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

임의로 배열 크기를 주어진 input 개수인 n 만큼 지정하고

이후에 answer 배열 크기를 지정, 데이터 저장.

 

반응형