1.问题
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:
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]
Explanation: [4,9] is also accepted.
Constraints:
- 1 <= nums1.length, nums2.length <= 1000
- 0 <= nums1[i], nums2[i] <= 1000
2. 解题思路
方法1:
1.创建一个 HashSet 集合用于存储交集元素。
2.遍历第一个数组,对于每个元素,在第二个数组中查找是否有相等的元素,如果有,将其加入集合中。
3.创建一个数组用于存储结果,并将集合中的元素存入数组中。
4.返回结果数组。
方法2:
排序加双指针
1.创建一个空的HashSet,用来保存结果;
2.对两个数组进行排序;
3.用双指针分别扫描两个数组,如果当前元素相等,则将其添加到结果集中,并移动双指针,如果不相等,则移动指针较小的那个;
4.将结果集转换为数组返回。
- 由于双指针遍历数组的时间复杂度为 O(n),因此总时间复杂度为 O(nlogn),其中 n 为两个数组的长度之和。
方法3:
使用哈希表来存储第一个数组中的元素
然后遍历第二个数组,对于每个元素,判断其是否在哈希表中出现过,如果出现过,将其加入结果集合中。
由于哈希表的查找时间复杂度为 O(1),因此总时间复杂度为 O(n),其中 n 为两个数组的长度之和
方法4:
使用了排序和二分查找
1.先对nums2进行排序
2.然后对nums1进行遍历,对于每个元素,使用二分查找在nums2中查找是否存在该元素,如果存在,将其加入集合中
3.最后将集合转换为数组返回。时间复杂度为O(mlogn),其中m和n分别是nums1和nums2的长度。
3. 代码
代码1:
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>(); //创建HashSet集合用于存储交集元素
for(int n1:nums1){ //遍历第一个数组
for(int n2:nums2){ //遍历第二个数组
if(n1==n2) set.add(n1); //如果两个数相等,则将其加入集合
}
}
int[] result = new int[set.size()]; //创建数组用于存储结果
int i=0;
for(int s:set){ //遍历集合,将集合中的元素存入数组
result[i++]=s;
}
return result; //返回结果数组
}
}
代码2:
public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
// 创建一个空的HashSet
Set<Integer> set = new HashSet<>();
// 对两个数组进行排序
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0, j = 0;
// 用双指针分别扫描两个数组
while (i < nums1.length && j < nums2.length) {
// 如果当前元素相等,则将其添加到结果集中,并移动双指针
if (nums1[i] == nums2[j]) {
set.add(nums1[i]);
i++;
j++;
}
// 如果不相等,则移动指针较小的那个
else if (nums1[i] < nums2[j]) {
i++;
} else {
j++;
}
}
// 将结果集转换为数组返回
int[] result = new int[set.size()];
int k = 0;
for (Integer num : set) {
result[k++] = num;
}
return result;
}
}
代码3:
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>(); // 创建一个HashSet对象set
Set<Integer> intersect = new HashSet<>(); // 创建一个HashSet对象intersect
for (int i = 0; i < nums1.length; i++) { // 遍历nums1数组
set.add(nums1[i]); // 将nums1数组中的所有元素加入set中
}
for (int i = 0; i < nums2.length; i++) { // 遍历nums2数组
if (set.contains(nums2[i])) { // 如果set中包含nums2中的元素
intersect.add(nums2[i]); // 将该元素加入intersect中
}
}
int[] result = new int[intersect.size()]; // 创建一个大小为intersect的长度的新数组result
int i = 0;
for (Integer num : intersect) { // 遍历intersect
result[i++] = num; // 将intersect中的元素存储到result数组中
}
return result; // 返回result数组
}
}
代码4:
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
// 使用 set 存储交集元素,避免重复
Set<Integer> set = new HashSet<>();
// 对 nums2 进行排序,方便使用二分查找
Arrays.sort(nums2);
// 遍历 nums1 数组
for (Integer num : nums1) {
// 如果 nums2 中包含 num 元素,则将其加入 set 中
if (binarySearch(nums2, num)) {
set.add(num);
}
}
// 将 set 中的元素转为数组
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;
}
}