题目如下:

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. ios入门之c语言篇——基本函数——4——数值交换函数

    一个常用函数,被整理出来,免得每次 都要写 参数返回值解析: 参数: *a:int*,需要交换值的第一个变量: *b:int*,需要交换值的第二个变量: 返回值: (无) 函数解析: swap(&am ...

  2. 想做Android Wear开发?你得先搞明白这四件事

    手环和手表的腕上穿戴之争,随着Apple Watch发布和Android Wear不断完善而告一段落.尽管续航上略有缺陷,但手表以其类似手机可扩展的生态环境赢得了众多巨头的支持. Google曾透露, ...

  3. 【POJ】2418 Hardwood Species

    简单字典树. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 128 ty ...

  4. Entropy (huffman) 优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1053 Huffman问题利用STL中的priority_queue解决: #include<stdio.h ...

  5. C#中实现邮件发送功能

    public static int sendmail(string to, string body,string subject) { try { int nContain = 0; ///添加发件人 ...

  6. Partition List ——LeetCode

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  7. 动态规划——E (LIS())最长上升子序列

    E - LIS Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  8. 高效算法——Bin Packing F - 贪心

      Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Descripti ...

  9. ubuntu 下修改host文件

    原文地址:http://www.cnblogs.com/kingcat/archive/2012/02/23/2364509.html 有些时候,我们需要让某些域名指向本地,来实现调试,下面介绍下ub ...

  10. tyvj P1517 飘飘乎居士的乌龟(最大流)

    P1517 飘飘乎居士的乌龟 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 飘飘乎居士养了乌龟.当然,这些乌龟是用来出售赚取利润的. 描述 飘飘乎居士的乌龟 ...