본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] 두 개 뽑아서 더하기

by 현장 2025. 2. 25.

-Code

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

class Solution {
    public int[] solution(int[] numbers) {
        List<Integer> answer = new ArrayList<>();
        int numLen = numbers.length;
        for (int i = 0; i < numLen; i++) {
            for (int j = i + 1; j < numLen; j++) {
                int numSum = numbers[i] + numbers[j];
                if (!answer.contains(numSum)) answer.add(numSum);
            }
        }
        Collections.sort(answer);
        return answer.stream().mapToInt(Integer::intValue).toArray();
    }
}