정수 n
이 매개변수로 주어질 때, n
이하의 홀수가 오름차순으로 담긴 배열을 return하도록 solution 함수를 완성해주세요.
n
≤ 100n | result |
---|---|
10 | [1, 3, 5, 7, 9] |
15 | [1, 3, 5, 7, 9, 11, 13, 15] |
입출력 #1
입출력 #1
using System;
using System.Collections.Generic;
public class Solution {
public int[] solution(int n) {
List<int> list = new List<int>();
for(int i = 1; i <= n; i++)
{
if(i % 2 == 1)
{
list.Add(i);
}
}
int[] answer = new int[list.Count];
for(int i = 0; i < list.Count; i++)
{
answer[i] = list[i];
}
return answer;
}
}
출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges