题目如下:

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 static int[] intersect(int[] nums1, int[] nums2) {

        List<Integer> reslut = new ArrayList<>();

        Arrays.sort(nums1);// 先进行数组排序
Arrays.sort(nums2); int position = 0;// 记录位置 for (int i = 0; i < nums2.length; i++) { if (position == nums1.length) {// 终止条件:如果位置已经是最后一个了,终止
break;
} for (int j = position; j < nums1.length; j++) {
if (position < nums1.length) {
if (nums2[i] == nums1[j]) {
position = j + 1;// 记录下当前相等的位置
reslut.add(nums2[i]);
break;
}
}
}
} int[] resultNums = new int[reslut.size()]; for (int i = 0; i < reslut.size(); i++) {
resultNums[i] = reslut.get(i);
} return resultNums; }

上面算法很简单,就是先对数组进行排序,然后遍历,记录相等时另一个数组的位置,然后放入list中。

leetcode修炼之路——350. Intersection of Two Arrays II的更多相关文章

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

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

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

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

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

随机推荐

  1. WPF感悟(1)

    原文地址:http://liutiemeng.blog.51cto.com/120361/91632 1.UI层与逻辑层要尽可能地剥离(解耦). 2.Routed Event和Command比Even ...

  2. Unity 网络斗地主 牌的一些算法

    Unity 网络斗地主  牌的一些算法 在这儿说一下,我的项目是用svn的方式,上传在https://v2.svnspot.com/18666451713.doudizhu这个svn上,大家可以下载T ...

  3. xp下删除windows7,无法删除windows7文件夹,无法删除windows7文件,双系统卸载,取得文件权限

    http://blog.csdn.net/lanmanck/article/details/5722050 ---------------------------------------------- ...

  4. c# 循环语句练习题;

    1. 求100以内质数的和 2. 兔子问题 3. 九九乘法表:   一行一行打印: 4. 有一张超大的纸:   纸质的厚度是0.01:   对折多少次,可以达到珠峰的高度:   按照8848来计算: ...

  5. Oracle 多版本控制

    SESSION 1: SQL> create table t 2 as 3 select * from all_users; Table created. SQL> variable x ...

  6. Linux Shell编程(8)——变量详解

    不同与许多其他的编程语言,Bash不以"类型"来区分变量.本质上来说,Bash变量是字符串,但是根据环境的不同,Bash允许变量有整数计算和比较.其中的决定因素是变量的值是不是只含 ...

  7. [Locked] Inorder Successor in BST

    Inorder Successor in BST Given a binary search tree and a node in it, find the in-order successor of ...

  8. “VICUTU威克多”高档男装

    "VICUTU威克多"高档男装   北京威克多制衣中心是一家从事高档男装设计.制造和销售为一体的股份服装企业.主要经营品牌为"VICUTU"男装系列,主导产品为 ...

  9. JEFF BANKS_百度百科

    JEFF BANKS_百度百科 JEFF BANKS

  10. linux 发邮件

      一. centos yum 安装 1. yum install mailx vim  /etc/nail.rc 添加网易163邮箱开放的需要认证的smtp服务器: set from=USER@16 ...