짝수는 싫어요

han.user();·2023년 3월 20일
0

프로그래머스

목록 보기
2/87
post-thumbnail

나의 답안

class Solution {
    public int[] solution(int n) {
       int[] answer = new int[(n+1)/2];

        for (int i = 1; i <= n; i++) {
            if(i%2!=0){
                answer[i/2]=i;
            }
        }
        return answer;
    }
}

처음에는 홀수값을 담을 배열을 생성하고, 홀수인 경우 차례대로 배열에 넣는다.

profile
I'm still hungry.

0개의 댓글