mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 02:53:31 +08:00
添加0349.两个数组的交集 Java版本
This commit is contained in:
@ -75,7 +75,37 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
```Java
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
class Solution {
|
||||
public int[] intersection(int[] nums1, int[] nums2) {
|
||||
if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
|
||||
return new int[0];
|
||||
}
|
||||
Set<Integer> set1 = new HashSet<>();
|
||||
Set<Integer> resSet = new HashSet<>();
|
||||
//遍历数组1
|
||||
for (int i : nums1) {
|
||||
set1.add(i);
|
||||
}
|
||||
//遍历数组2的过程中判断哈希表中是否存在该元素
|
||||
for (int i : nums2) {
|
||||
if (set1.contains(i)) {
|
||||
resSet.add(i);
|
||||
}
|
||||
}
|
||||
int[] resArr = new int[resSet.size()];
|
||||
int index = 0;
|
||||
//将结果几何转为数组
|
||||
for (int i : resSet) {
|
||||
resArr[index++] = i;
|
||||
}
|
||||
return resArr;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
Reference in New Issue
Block a user