나의 풀이
class Solution {
public int[] solution(int[] num_list) {
int[] answer = new int[num_list.length];
int num = num_list.length-1;
for(int i=num; i>=0; i--){
answer[(i-num)*-1]=num_list[i];
}
return answer;
}
}
i-num=음수 *-1 하면 양수가 될테니 거꾸로 루프해서 풀었다
좋아요를 많이받은 풀이
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Arrays;
class Solution {
public int[] solution(int[] numList) {
List<Integer> list = Arrays.stream(numList).boxed().collect(Collectors.toList());
Collections.reverse(list);
return list.stream().mapToInt(Integer::intValue).toArray();
}
}
- boxed()
boxed()메소드는 intStream과 같이 원시타입에 대한 스트림 지원을 클래스타입(ex:IntStream -> Stream<Integer>)로 전환해준다. - Collections.reverse(list)
파라마티러 받은 list를 거꾸로 뒤집는다.
'알고리즘' 카테고리의 다른 글
[프로그래머스] 최댓값 만들기 (1) (0) | 2023.09.24 |
---|---|
[프로그래머스] 가위바위보 (0) | 2023.09.21 |
[프로그래머스] 문자열 뒤집기 (0) | 2023.09.21 |
[프로그래머스] 배열두배 만들기 (0) | 2023.09.20 |
[프로그래머스] 최빈값 구하기 .java (0) | 2023.09.19 |