Leetcode: Intersection of Two Arrays

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note:
Each element in the result must be unique.
The result can be in any order.

Use two hash sets

Time complexity: O(n)

注意Set<Integer> set = new HashSet<>(); 第二个泛型可以不写出

 1 public class Solution {
 2     public int[] intersection(int[] nums1, int[] nums2) {
 3         Set<Integer> set = new HashSet<>();
 4         Set<Integer> intersect = new HashSet<>();
 5         for (int i = 0; i < nums1.length; i++) {
 6             set.add(nums1[i]);
 7         }
 8         for (int i = 0; i < nums2.length; i++) {
 9             if (set.contains(nums2[i])) {
10                 intersect.add(nums2[i]);
11             }
12         }
13         int[] result = new int[intersect.size()];
14         int i = 0;
15         for (Integer num : intersect) {
16             result[i++] = num;
17         }
18         return result;
19     }
20 }

Iterator iter = set2.iterator();
for (int i=0; i<res.length; i++) {
res[i] = (int)iter.next();
}

用iterator也可以,但要注意.next()之后要转换为int

 

Sort both arrays, use two pointers

Time complexity: O(nlogn)

 1 public class Solution {
 2     public int[] intersection(int[] nums1, int[] nums2) {
 3         Set<Integer> set = new HashSet<>();
 4         Arrays.sort(nums1);
 5         Arrays.sort(nums2);
 6         int i = 0;
 7         int j = 0;
 8         while (i < nums1.length && j < nums2.length) {
 9             if (nums1[i] < nums2[j]) {
10                 i++;
11             } else if (nums1[i] > nums2[j]) {
12                 j++;
13             } else {
14                 set.add(nums1[i]);
15                 i++;
16                 j++;
17             }
18         }
19         int[] result = new int[set.size()];
20         int k = 0;
21         for (Integer num : set) {
22             result[k++] = num;
23         }
24         return result;
25     }
26 }

 

 

Binary search

Time complexity: O(nlogn)

 1 public class Solution {
 2     public int[] intersection(int[] nums1, int[] nums2) {
 3         Set<Integer> set = new HashSet<>();
 4         Arrays.sort(nums2);
 5         for (Integer num : nums1) {
 6             if (binarySearch(nums2, num)) {
 7                 set.add(num);
 8             }
 9         }
10         int i = 0;
11         int[] result = new int[set.size()];
12         for (Integer num : set) {
13             result[i++] = num;
14         }
15         return result;
16     }
17     
18     public boolean binarySearch(int[] nums, int target) {
19         int low = 0;
20         int high = nums.length - 1;
21         while (low <= high) {
22             int mid = low + (high - low) / 2;
23             if (nums[mid] == target) {
24                 return true;
25             }
26             if (nums[mid] > target) {
27                 high = mid - 1;
28             } else {
29                 low = mid + 1;
30             }
31         }
32         return false;
33     }
34 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/6096747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值