List<Integer> res = new ArrayList<>();
Arrays.sort(nums1);
Arrays.sort(nums2);
int i1 = 0;
int i2 = 0;
while (i1<nums1.length&&i2<nums2.length)
{
if (nums1[i1]<nums2[i2])
i1++;
else if (nums1[i1]>nums2[i2])
i2++;
else
{
res.add(nums1[i1]);
i1++;
i2++;
}
}
int[] num = new int[res.size()];
for (int i = 0; i < num.length; i++) {
num[i] = res.get(i);
}
return num;

受上一题的影响,本来想用hashset解决,但是发现不行,就换了排序然后遍历的方法,如果不相等,小的数的下边++,相等就添加

[leetcode350]Intersection of Two Arrays II求数组交集的更多相关文章

  1. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  2. [LintCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...

  3. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  4. [LeetCode] 349. Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  5. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  6. 26. leetcode 350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...

  7. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

  8. 【leetcode】350. Intersection of Two Arrays II

    problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...

  9. LeetCode_350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Easy Given two arrays, write a function to compute their intersec ...

随机推荐

  1. 面试官:说一下List排序方法

    1. 前言 排序算是比较高频的面试题了,节前面试了的两家公司都有问到排序问题,整理后分享给大家(文末见总结). 通常我们想到实现排序就是 Collections 工具类的 sort() 方法,而 so ...

  2. CSP2020复赛游记

    CSP2020复赛游记 由于本蒟蒻侥幸通过PJ和TG的分数线并且侥幸的拿了一等,所以侥幸的来参加复赛 11.04~11.05 期中考,挂 11.06 对答案,炸 11.07 开始了第一次CSP复赛 坐 ...

  3. 使用Docker快速部署各类服务

    使用Docker快速部署各类服务 一键安装Docker #Centos环境 wget -O- https://gitee.com/iubest/dinstall/raw/master/install. ...

  4. springboot:读取application.yml文件

    现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...

  5. PyQt(Python+Qt)学习随笔:QSpinBox数字设定部件简介

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 在输入部件中,数字调整框QSpinBox是个很实用 ...

  6. PyQt(Python+Qt)学习随笔:QDockWidget停靠部件floating和features属性

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 1.floating属性 floating属性表示QDockWidge ...

  7. PyQt(Python+Qt)学习随笔:Qt Designer中部件的mouseTracking和tabletTracking属性

    在Qt Designer中的部件属性设置中,有mouseTracking和tabletTracking两个属性,这两个属性是跟踪鼠标或平板触控笔的移动轨迹的. 一.mouseTracking mous ...

  8. 基础篇——怎么使用PowerDesigner进行数据库初始化

    1.使用PowerDesigner打开设计好的 .pdm文件 2.点击工具栏中的Database-->Database Generation-->Preview,就可以看到生成的创建数据库 ...

  9. centos7 yum搭建lamp

    环境 系统:centos7 安装apache #yum 安装apache [root@localhost ~]# yum install httpd httpd-devel #启动httpd服务 [r ...

  10. js- 对象的连续调用

    var jack = { somke : function (){ console.log('I was in the somkeing...cool..'); return this; }, dri ...