문제링크 : https://programmers.co.kr/learn/courses/30/lessons/68644?language=java
코드
import java.util.*;
class Solution {
public static void main(String[] args) {
tester(new int[] {2,1,3,4,1},new int[] { 2,3,4,5,6,7});
tester(new int[] {5,0,2,7},new int[] {2,5,7,9,12});
return;
}
public static void tester(int[] numbers, int[] result) {
Solution s = new Solution();
int[] ret = s.solution(numbers);
for (int i=0 ; i<result.length ; i++) {
if(ret[i] != result[i]) {
System.out.println("Failed!");
return;
}
}
System.out.println("OK");
}
public int[] solution(int[] numbers) {
//ArrayList<Integer> buf = new ArrayList<>();
ArrayList<Integer> buf = new ArrayList<>();
int n = numbers.length;
for(int i=0 ; i<n ; i++) {
for(int j=i+1 ; j<n ; j++) {
int temp = numbers[i]+ numbers[j];
if(buf.indexOf(temp) == -1) {
buf.add(temp);
}
}
}
int[] answer = new int[buf.size()];
for(int i=0 ; i<buf.size() ; i++) {
answer[i] = buf.get(i);
}
Arrays.sort(answer);
return answer;
}
}