(Python, Java) 프로그래머스 - 두 개 뽑아서 더하기

[문제 링크]

Python 풀이

from itertools import combinations


def solution(numbers):
    answer = []

    for comb in combinations(numbers, 2):
        answer.append(comb[0] + comb[1])

    answer = list(set(answer))
    answer.sort()
    return answer

Java 풀이

class Solution {
    public int[] solution(int[] numbers) {
        Set<Integer> set = new TreeSet<>();
        int length = numbers.length;
        for (int i = 0; i < length - 1; i++) {
            for (int j = i + 1; j < length; j++) {
                set.add(numbers[i] + numbers[j]);
            }
        }
        return set.stream().mapToInt(Integer::intValue).toArray();
    }
}

© 2021. By Backtony