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

2023. 2. 27. 18:06프로그래머스 - JAVA

문제

정수 n이 매개변수로 주어질 때, n 이하의 홀수가 오름차순으로 담긴 배열을 return하도록 solution 함수를 완성해주세요.

 

조건

1 ≤ n ≤ 100

 

코드

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

 

 

 

 

출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges