https://school.programmers.co.kr/learn/courses/30/lessons/68644
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.util.*;
class Solution {
public int[] solution(int[] numbers) {
Set<Integer> set = new HashSet<>();
for(int i = 0; i < numbers.length - 1; i++){
for(int j = i + 1; j < numbers.length; j++){
set.add(numbers[i] + numbers[j]);
}
}
int[] answer = new int[set.size()];
ArrayList<Integer> list = new ArrayList<>(set);
Collections.sort(list);
for(int i = 0; i < list.size(); i++) answer[i] = list.get(i);
return answer;
}
}
풀이 회고
중복을 피하기 위해서 HashSet 자료구조를 사용하였다.
set.stream().sorted().mapToInt(Integer::intValue).toArray();
스트림 문법을 사용하면 List를 사용하지 않아도 된다!
'Algorithm > 프로그래머스 코딩테스트 문제풀이전략' 카테고리의 다른 글
[프로그래머스] Lv.1 문자열 내림차순으로 배치하기 (0) | 2024.05.02 |
---|---|
[프로그래머스] Lv.2 H-index (0) | 2024.05.02 |
[프로그래머스] Lv.1 k번째 수 (0) | 2024.05.02 |
[프로그래머스] Lv.2 소수 찾기 (0) | 2024.04.24 |
[프로그래머스] Lv.1 모의고사 (0) | 2024.04.20 |