-
프로그래머스 - 두 개 뽑아서 더하기알고리즘 2024. 4. 13. 11:19
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 = set.stream().mapToInt(Integer::intValue).toArray(); Arrays.sort(answer); return answer; } }
중복이 없는 Set을 이용하여, 배열을 순회해 두 수를 더해 set에 넣고 배열로 변환하여 정렬해 반환했습니다.
'알고리즘' 카테고리의 다른 글
프로그래머스 - 콜라 문제 (0) 2024.04.14 프로그래머스 - 푸드 파이트 대회 (0) 2024.04.13 프로그래머스 - 비밀지도 (0) 2024.04.12 프로그래머스 - 문자열 내 마음대로 정렬하기 (0) 2024.04.12 프로그래머스 - 가장 가까운 같은 글자 (0) 2024.04.12