Home 프로그래머스 두 개 뽑아서 더하기
Post
Cancel

프로그래머스 두 개 뽑아서 더하기

프로그래머스 두 개 뽑아서 더하기

풀이

  • 소요 시간 약 5분

내가 푼 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;

class Solution {
    public int[] solution(int[] numbers) {
        Arrays.sort(numbers);
        ArrayList<Integer> list = new ArrayList<>();
        
        for(int i=0; i<numbers.length-1; i++){
            for(int j=i+1; j<numbers.length; j++){
                int sum = numbers[i] + numbers[j];
                
                if(!list.contains(sum)) list.add(sum);
            }
        }
        
        return list.stream().sorted().mapToInt(Integer::intValue).toArray();
    }
}
This post is licensed under CC BY 4.0 by the author.