349. Intersection of Two Arrays

网友投稿 622 2022-10-09

349. Intersection of Two Arrays

349. 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.

public class Solution { public int[] intersection(int[] nums1, int[] nums2) { Arrays.sort(nums1); Arrays.sort(nums2); ArrayList list = new ArrayList(); for(int i=0; i0 && nums1[i]!=nums1[i-1])){ if(Arrays.binarySearch(nums2, nums1[i])>-1){ list.add(nums1[i]); } } } int[] result = new int[list.size()]; int k=0; for(int i: list){ result[k++] = i; } return

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Go生成海报,小程序分享图片海报,小程序分享朋友圈(小程序制作海报并分享到朋友圈)
下一篇:Rexdb-tester- 数据持久层框架性能测试程序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~