Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- 위상정렬
- 99클럽
- 코테
- 99클럽 #코딩테스트준비 #개발자취업 #항해99 #til
- til
- 알고리즘
- Sort
- 자료구조
- Java
- JPA
- generic class
- 가상컴퓨팅
- dbms
- 정렬
- 코딩테스트준비
- js
- javascript
- LeetCode
- Algorithm
- sql
- 개발자취업
- 항해99
- mysql
- 코딩테스트
- 크루스칼
- spring
- jsp
- python
- 공개키 암호화
- DB
Archives
- Today
- Total
PLOD
[Leetcode] 349. Intersection of Two Arrays 본문
🔗 문제 링크
https://leetcode.com/problems/intersection-of-two-arrays/description/
Intersection of Two Arrays - LeetCode
Can you solve this real interview question? Intersection of Two Arrays - Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order. Example 1: In
leetcode.com
✅ 코드
import java.io.*;
import java.util.*;
class Solution {
static Set<Integer> set;
public int[] intersection(int[] nums1, int[] nums2) {
set = new HashSet<>();
int n = nums1.length;
int m = nums2.length;
Arrays.sort(nums1);
Arrays.sort(nums2);
if(n >= m){
for(int i = 0; i < m ; i++){
if(Arrays.binarySearch(nums1,nums2[i]) > -1){
set.add(nums2[i]);
}
}
}else{
for(int i = 0 ; i < n ; i++){
if(Arrays.binarySearch(nums2,nums1[i]) > -1){
set.add(nums1[i]);
}
}
}
int[] answer = set.stream()
.mapToInt(Integer::intValue)
.toArray();
return answer;
}
}
💡 배운 점 & 느낀 점
1. Arrays.binarySearch → 배열에서 이진탐색을 하기 위해서는 반드시 먼저 정렬(Arrays.sort())를 진행해줘야 한다
'대외 활동 및 IT 지식 > 알고리즘 문제 풀이 정리' 카테고리의 다른 글
| [백준] 캠프가는 영식(1590) (0) | 2026.04.13 |
|---|---|
| [백준] 브실이의 입시전략(29723) (0) | 2026.04.10 |
| [백준] 단어 정렬(1181) (0) | 2026.04.10 |
| [Programmers] 조건에 맞는 개발자 찾기(MySQL) (0) | 2026.03.23 |
| [백준] 평행선(2358) (0) | 2026.03.23 |
Comments