关闭

Leetcode no. 350

114人阅读 评论(0) 收藏 举报
分类:

350. Intersection of Two Arrays II


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.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1's size is small compared to num2's size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

public class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        Map<Integer, Integer> map= new HashMap<>();
        List<Integer> list= new LinkedList<>();
        for (int ele: nums1) {
            if (map.containsKey(ele)) map.put(ele, map.get(ele)+1);
            else map.put(ele, 1);
        }
        for (int ele: nums2) {
            if (map.containsKey(ele) && map.get(ele)>0){
                list.add(ele);
                map.put(ele, map.get(ele)-1);
            }
        }
        int[] res= new int[list.size()];
        int i=0;
        Iterator it= list.iterator();
        while (it.hasNext()){
            res[i++]=(Integer) it.next();
        }
        return res;
    }
}


0
0
查看评论

[leetcode] 350. Intersection of Two Arrays II 解题报告

题目链接: https://leetcode.com/problems/intersection-of-two-arrays-ii/ Given two arrays, write a function to compute their intersection. Example: ...
  • qq508618087
  • qq508618087
  • 2016-05-23 13:21
  • 1992

LeetCode 350. Intersection of Two Arrays II(数组交集)

原题网址:https://leetcode.com/problems/intersection-of-two-arrays-ii/ Given two arrays, write a function to compute their intersection. Example: Given&...
  • jmspan
  • jmspan
  • 2016-05-25 01:35
  • 755

leetcode_350 Intersection of Two Arrays II

题目分析:给定两个数组,求数组的交集。输出结果中的元素不唯一,输出数组可以无序。 解题思路:1)对数组nums1进行排序;2)对数组nums2进行排序;3)遍历数组nums1和nums2中元素,并比较对应的元素,若相等,则将其保存到输出结果中,并变化两个数组对应的索引 不等,则变化较小元素对应的索引...
  • yzhang6_10
  • yzhang6_10
  • 2016-05-28 18:17
  • 2212

error:stray'\350'in program

有非标ascII的字符,字符一般都是全角符号什么的比如{ } 和{} ,和,
  • irlwh
  • irlwh
  • 2017-01-05 19:32
  • 542

Leetcode no. 231

231. Power of Two Given an integer, write a function to determine if it is a power of two. public class Solution { public boolean is...
  • SmithZz
  • SmithZz
  • 2016-05-28 22:55
  • 120

Leetcode no. 16

16. 3Sum Closest    Given an array S of n integers, find three integers in S such that the sum is cl...
  • SmithZz
  • SmithZz
  • 2016-04-26 11:38
  • 120

Leetcode no. 80

80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? Fo...
  • SmithZz
  • SmithZz
  • 2016-04-08 10:48
  • 173

Leetcode no. 43

43. Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers ca...
  • SmithZz
  • SmithZz
  • 2016-05-28 22:26
  • 138

Leetcode no. 258

258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: ...
  • SmithZz
  • SmithZz
  • 2016-06-01 00:51
  • 101

Leetcode no. 21

21. Merge Two Sorted Lists   Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together ...
  • SmithZz
  • SmithZz
  • 2016-04-26 10:38
  • 123
    个人资料
    • 访问:15188次
    • 积分:1239
    • 等级:
    • 排名:千里之外
    • 原创:111篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章分类