[java] 프로그래머스 - 짝수는 싫어요

세상을 바꾸는 개발자·2023년 3월 23일
0

[문제링크 - 프로그래머스 - 짝수는 싫어요] https://school.programmers.co.kr/learn/courses/30/lessons/120813

class Solution {
    public int[] solution(int n) {
        int[] answer;
        int num = 0;

        if(n%2 == 0)
            answer = new int[n/2];
        else
            answer = new int[n/2+1];

        for(int i=1; i<=n; i+=2)
            answer[num++] = i;

        return answer;
    }
}
profile
초심 잃지 않기

0개의 댓글