개발 일지
2248. Intersection of Multiple Arrays (다중 배열 교차점) 본문
2D 정수 배열 nums[i]가 비어 있지 않은 양의 정수 배열인 경우 오름차순으로 정렬된 각 nums 배열에 있는 정수 목록을 반환합니다.
예시 1:
입력: nums = [3,1,2,4,5], [1,2,3,4], [3,4,5,6]
출력: [3,4]
설명: 각 num[0] = [3,1,2,4,5], nums[1] = [1,2,3,4], nums[2] = [3,4,5,6]의 정수는 3과 4이므로 [3,4]를 반환합니다.
예시 2:
입력: nums = [[1,2,3], [4,5,6]
출력: []
설명: num[0] 및 num[1]에 정수가 없으므로 빈 목록 []을 반환합니다.
조건:
- 1 <= nums.length <= 1000
- 1 <= sum(nums[i].length) <= 1000
- 1 <= nums[i][j] <= 1000
정답:
public class Solution {
public IList<int> Intersection(int[][] nums) {
IList<int> result = new List<int>();
bool check;
for (int i = 0; i < nums[0].Length; i++)
{
check = true;
for (int j = 1; j < nums.Length; j++)
{
if (Array.IndexOf(nums[j], nums[0][i]) == -1)
{
check = false;
}
}
if (check)
{
result.Add(nums[0][i]);
}
}
return result.OrderBy(f=>f).ToList();
}
}
해설:
0번 배열에서 순서대로 모든 배열을 탐색하고 존재할 경우에만 값을 추가해주어 값을 반환해주었습니다.
'코딩 테스트 > LeetCode' 카테고리의 다른 글
1528. Shuffle String (문자열 셔플) (2) | 2023.06.09 |
---|---|
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers (최소 십진수 이진수로 분할) (0) | 2023.06.07 |
136. Single Number (단일 번호) (0) | 2023.06.04 |
1437. Check If All 1's Are at Least Length K Places Away (1 항목 모두 길이 K 이상 떨어져 있는지 확인) (0) | 2023.05.31 |
985. Sum of Even Numbers After Queries (쿼리 후 짝수의 합) (0) | 2023.05.24 |