[LeetCode] 349. Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Note:
- Each element in the result must be unique.
- The result can be in any order.
解法:由于结果中要求元素是唯一的,所以用set来统计num1 中的数字。再循环num2中的数字,在set中存在就记录到结果中,同时从set中删除。
Java: HashSet, T: O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Solution { public int [] intersection( int [] nums1, int [] nums2) { HashSet<Integer> set = new HashSet<Integer>(); ArrayList<Integer> res = new ArrayList<Integer>(); //Add all elements to set from array 1 for ( int i = 0 ; i< nums1.length; i++) set.add(nums1[i]); for ( int j = 0 ; j < nums2.length; j++) { // If present in array 2 then add to res and remove from set if (set.contains(nums2[j])) { res.add(nums2[j]); set.remove(nums2[j]); } } // Convert ArrayList to array int [] arr = new int [res.size()]; for ( int i= 0 ; i < res.size(); i++) arr[i] = res.get(i); return arr; } } |
Java: Hashset T: O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class Solution { public int [] intersection( int [] nums1, int [] nums2) { Set<Integer> set = new HashSet<>(); Set<Integer> intersect = new HashSet<>(); for ( int i = 0 ; i < nums1.length; i++) { set.add(nums1[i]); } for ( int i = 0 ; i < nums2.length; i++) { if (set.contains(nums2[i])) { intersect.add(nums2[i]); } } int [] result = new int [intersect.size()]; int i = 0 ; for (Integer num : intersect) { result[i++] = num; } return result; } } |
Java: two points, T: O(nlogn)
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 | public class Solution { public int [] intersection( int [] nums1, int [] nums2) { Set<Integer> set = new HashSet<>(); Arrays.sort(nums1); Arrays.sort(nums2); int i = 0 ; int j = 0 ; while (i < nums1.length && j < nums2.length) { if (nums1[i] < nums2[j]) { i++; } else if (nums1[i] > nums2[j]) { j++; } else { set.add(nums1[i]); i++; j++; } } int [] result = new int [set.size()]; int k = 0 ; for (Integer num : set) { result[k++] = num; } return result; } } |
Java: Binary Search, T: O(nlogn)
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 32 33 34 | public class Solution { public int [] intersection( int [] nums1, int [] nums2) { Set<Integer> set = new HashSet<>(); Arrays.sort(nums2); for (Integer num : nums1) { if (binarySearch(nums2, num)) { set.add(num); } } int i = 0 ; int [] result = new int [set.size()]; for (Integer num : set) { result[i++] = num; } return result; } public boolean binarySearch( int [] nums, int target) { int low = 0 ; int high = nums.length - 1 ; while (low <= high) { int mid = low + (high - low) / 2 ; if (nums[mid] == target) { return true ; } if (nums[mid] > target) { high = mid - 1 ; } else { low = mid + 1 ; } } return false ; } } |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Solution( object ): def intersection( self , nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ res = [] s = set () for num in nums1: s.add(num) for num in nums2: if num in s: res.append(num) s.remove(num) return res |
Python:
1 2 3 4 5 6 7 8 9 10 | class Solution( object ): def intersection( self , nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ nums1 = set (nums1) nums2 = set (nums2) return list (nums1&nums2) |
C++:
1 2 3 4 5 6 7 8 9 10 | class Solution { public : vector< int > intersection(vector< int >& nums1, vector< int >& nums2) { set< int > s(nums1.begin(), nums1.end()), res; for ( auto a : nums2) { if (s.count(a)) res.insert(a); } return vector< int >(res.begin(), res.end()); } }; |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Solution { public : vector< int > intersection(vector< int >& nums1, vector< int >& nums2) { vector< int > res; int i = 0, j = 0; sort(nums1.begin(), nums1.end()); sort(nums2.begin(), nums2.end()); while (i < nums1.size() && j < nums2.size()) { if (nums1[i] < nums2[j]) ++i; else if (nums1[i] > nums2[j]) ++j; else { if (res.empty() || res.back() != nums1[i]) { res.push_back(nums1[i]); } ++i; ++j; } } return res; } }; |
类似题目:
[LeetCode] 350. Intersection of Two Arrays II 两个数组相交II
[LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集
All LeetCode Questions List 题目汇总
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构