1. 기본 for문 ( ++ )
class Solution {
public int[] solution(int[] num_list) {
int[] answer = new int[num_list.length];
for(int i=0;i<num_list.length;i++) {
answer[num_list.length-1-i] = num_list[i];
}
return answer;
}
}
2. 기본 for문 ( -- )
class Solution {
public int[] solution(int[] num_list) {
int[] answer = new int[num_list.length];
int startIdx = 0;
for(int i= num_list.length-1; i>=0; i--) {
answer[startIdx] = num_list[i];
startIdx++;
}
return answer;
}
}
3. Collections.reverse
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.List;
class Solution {
public int[] solution(int[] num_list) {
// int[ ] => list
List<Integer> list = Arrays.stream(num_list).boxed().collect(Collectors.toList());
Collections.reverse(list);
// list => int[ ]
return list.stream().mapToInt(Integer::intValue).toArray();
}
}
배열 뒤집기 알고리즘 나중에 유용할 것 같아 따로 모아두기..!