using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int[] solution(int[] numbers) {
List<int> list = new List<int>();
int sum;
for(int i = 0; i < numbers.Length; i++)
{
for (int j = i + 1; j < numbers.Length; j++)
{
sum = numbers[i] + numbers[j];
list.Add(sum);
}
}
List<int> list2 = new List<int>();
list2 = list.Distinct().ToList();
list2.Sort();
int[] answer = list2.ToArray();
return answer;
}
}
* Distinct().ToList() 함수로 중복 데이터를 삭제
다른 풀이
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int[] solution(int[] numbers) {
List<int> list = new List<int>();
int sum;
for(int i = 0; i < numbers.Length; i++)
{
for (int j = i + 1; j < numbers.Length; j++)
{
sum = numbers[i] + numbers[j];
if(!list.Contains(sum))
{
list.Add(sum);
}
}
}
list.Sort();
int[] answer = list.ToArray();
return answer;
}
}
* Contatins 함수로 쉽게 데이터가 list에 존재하는지 확인 가능
'C# 알고리즘 코드카타' 카테고리의 다른 글
51. 푸드 파이트 대회 (0) | 2023.12.06 |
---|---|
50. 가장 가까운 같은 글자 (1) | 2023.12.06 |
48. K번째수 (1) | 2023.12.04 |
47. 문자열 내 마음대로 정렬하기 (1) | 2023.11.29 |
46. 숫자 문자열과 영단어 (1) | 2023.11.28 |