정수 n이 매개변수로 주어질 때, n 이하의 홀수가 오름차순으로 담긴 배열을
return하도록 solution 함수를 완성해주세요.
1 ≤ n ≤ 100
class Solution {
public int[] solution(int n) {
int[] answer = new int[(n+1)/2];
//int 타입이므로 홀수값이 입력된다.
for(int i = 1; i <= n ; i++){
if(i % 2 == 1){
answer[i/2] += i; //차례대로 홀수값이 입력된다.
}
}
return answer;
}
}
import java.util.Arrays;
class Solution {
public int[] solution(int n) {
int[] answer = {};
int[] result = new int[(n+1)/2];
for(int i = 0; i <= n ; i++){
if(i % 2 == 1){
result[i] += i;
}
}
result[i] = answer;
Arrays.sort(answer);
return answer;
}
}
오름차순 배열에 꽂혀서 Arrays를 import했고
for문 안에서 answer을 어떻게 대입시켜야 할지 모르겠어서
result를 따로 변수 선언해서 만들었는데 이게 더 복잡하게 만들었다...