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

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

Note:

    • Each element in the result should appear as many times as it shows in both arrays.
    • The result can be in any order.

代码如下:

 public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
List<Integer> list=new ArrayList<>(); Arrays.sort(nums1);
Arrays.sort(nums2); for(int i=0,j=0;i<nums1.length&&j<nums2.length;)
{
if(nums1[i]==nums2[j])
{
list.add(nums1[i]);
i++;
j++;
}
else if(nums1[i]<nums2[j])
i++;
else if(nums1[i]>nums2[j])
j++;
}
int[] result=new int[list.size()];
for(int i=0;i<list.size();i++)
result[i]=list.get(i); return result; }
}

350. Intersection of Two Arrays II的更多相关文章

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

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

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

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

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

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

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

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

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

  6. 【一天一道LeetCode】#350. Intersection of Two Arrays II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

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

  8. leetcode349 350 Intersection of Two Arrays & II

    """ Intersection of Two Arrays Given two arrays, write a function to compute their in ...

  9. LeetCode 350. Intersection of Two Arrays II

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

  10. (Collection)350. Intersection of Two Arrays II

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

随机推荐

  1. UVa 11375 - Matches

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. linux 安装vpn

    机器环境 centos Linux  wdcp安装脚本和步骤第一步: wget http://www.huzs.net/soft/pptp_onekey/pptpd6.sh sh pptpd6.sh ...

  3. Gartner报告:多数CIO还未对数字化做好准备

    数字化经济时代已经来临.对于消费者而言,这意味着他们能够随时随地以更加丰富多彩的方式与虚拟世界和现实世界进行互动.对于企业而言,这意味着它们的运营将发生巨大变化,同时也有机会更加深入地了解客户并将这些 ...

  4. Problem C HDU 5224

    Description There is a piece of paper in front of Tom, its length and width are integer. Tom knows t ...

  5. Problem A CodeForces 556A

    Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think ab ...

  6. Android获取手机设备识别码(IMEI)和手机号码

    最近看了下获取手机设备ID和手机信息以及SIM的信息例子,主要还是借鉴别人的,现在自己写一下,算是巩固加深了,也希望能给大家一个参考 必要的条件还是一部真机,SIM卡或者UIM卡. 首先,在Andro ...

  7. 虚拟机无法上网的问题:无法启动VMnet0等问题

    虚拟机无法上网,由于之前安装过虚拟机,后来将它卸载了,然后重新安装,最后出现了虚拟机无法上网.刚开始以为是系统的原因,于是就通过linux命令查看系统里面的网卡时是否启动,如:/etc/init.d/ ...

  8. JS中 window的用法

    1.window.location.reload();作用是刷新当前页面

  9. [IE兼容性] Table 之边框 (IE6 IE7 IE8(Q) 中 cellspacing 属性在重合的边框模型的表格中仍然有效)

    在 IE6 IE7 IE8(Q) 中,在通过 border-collapse:collapse 使用表格的重合边框模型后,其 cellspacing 属性仍然有效: 在 其他浏览器 中,此时的 cel ...

  10. 迪士尼黑科技:爬墙机器人 VertiGo

    12 月 30 日,迪士尼研发出的一款爬墙机器人曝光了一段有趣的视频.从视频里可看出这个机器人碰到墙壁时迅速地作出反应爬了上去. 据了解,这个爬墙机器人名叫 VertiGo,由迪士尼研究中心和苏黎世联 ...