350. Intersection of Two Arrays II(LeetCode)

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 nums2‘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?

       1 class Solution {
       2 public:
       3     vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
       4         sort(nums1.begin(), nums1.end());
       5         sort(nums2.begin(), nums2.end());
       6         int len1 = nums1.size();
       7         int len2 = nums2.size();
       8
       9         int i = 0;
      10         int j = 0;
      11         vector<int> vet;
      12         while (i < len1&&j < len2)
      13         {
      14             if (nums1[i] == nums2[j])
      15             {
      16                 vet.push_back(nums1[i]);
      17                 i++;
      18                 j++;
      19
      20             }
      21             else
      22             {
      23                 if (nums1[i] < nums2[j])
      24                 {
      25                     i++;
      26                 }
      27                 else
      28                 {
      29                     j++;
      30                 }
      31             }
      32         }
      33         return vet;
      34     }
      35 };
时间: 2024-11-05 04:55:24

350. Intersection of Two Arrays II(LeetCode)的相关文章

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

【leetcode】350. Intersection of Two Arrays II

problem 350. Intersection of Two Arrays II 参考 1. Leetcode_350. Intersection of Two Arrays II; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10430296.html

119. Pascal&#39;s Triangle II(LeetCode)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? class Solution { public: vector<int> getRow(int rowIndex) { vector<int&

leetcode349 350 Intersection of Two Arrays &amp; II

1 """ 2 Intersection of Two Arrays 3 Given two arrays, write a function to compute their intersection. 4 Example 1: 5 Input: nums1 = [1,2,2,1], nums2 = [2,2] 6 Output: [2] 7 Example 2: 8 Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 9 Output:

LeetCode 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 ord

[LeetCode] NO. 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]. [题目解析] 这是对于求两个数组交集的延伸,之前求数组交集重复元素不包含在交集内,用set来求.那么这个问题,就可以用hashmap来解决.如下. public int[] intersect(int[] nums1, int

leetcode修炼之路——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

Leetcode 350. Intersection of Two Arrays II JAVA语言

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 or

【LeetCode】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]. 相比较I,这个题目允许答案出现重复的.想法就是做一个map统计nums1元素出现次数,然后nums2每次遍历都看看元素出现次数. class Solution { public: vector<int> intersect(