The front end of the algorithm the intersection of two arrays leetcode 350. II

# Front end of the algorithm the intersection of two arrays leetcode 350. II

Title Description

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

Example 1:

输入: nums1 = [1,2,2,1], nums2 = [2,2]
输出: [2,2]

Example 2:

输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出: [4,9]

Description:

  • The output frequency of each element appears, should be consistent with the number of elements that appear in both arrays.
  • We can not consider the order of output.

Advanced:

  • If given array is already sorted it? How will you optimize your algorithm?
  • If many nums1 size smaller than nums2, which method is better?
  • If the element nums2 stored on disk, the disk memory is limited and you can not be loaded once all the elements into memory, how can you do?

350. The two arrays of intersection II

Overview

This question can be seen as a traditional mapping problem, we need to know how many times each value occurs, mappings between <element occurrences>, first count the number of array elements appear in all 1, and then through the array 2 can be, If the array elements 2 and the presence of greater than 1, one and the same has been located, and if the value is equal to 1, delete [ 1 ]

prompt

Hash table, double pointer, violence

Resolve

Solution one: hash table

Time complexity of O (n)
first with a first array element Hashmap record key on the [], and the number of occurrences of the value in []. Then traverse the second array, if found corresponding element, add this element to the return array. If the value is greater than 1, the value in the HashMap value minus 1, has been located a the same. If the value is equal to 1, then remove the element. [ 2 ]

Solution two: the double pointer

The two ordered arrays, two pointers initial value is 0, the comparison element is equal to the two pointers are equal, returns into the array, while two forward pointers, the pointer is not equal to a small forward elements, if they are equal, then certainly compared the elements of no use, two hands forward, when unequal because of the larger elements are present in smaller array so the small forward element of the array pointer

Solution three: Violence Act

Time complexity of O (n ^ 2)
traverse the first array, the second array and then to find indexOfwhether there is current element

algorithm

/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var intersect = function (nums1, nums2) {
// hash法
  const [hash,res] = [new Map(),[]]
  nums1.forEach(el=>{
    if (hash.has(el)) {
      hash.set(el, hash.get(el) + 1)
    } else {
      hash.set(el, 1)
    }
  })
  nums2.forEach(el=>{
    const hashKey = hash.get(el)
    if (hash.has(el)) {
      res.push(el)
      if (hashKey > 1) {
        hash.set(el, hashKey - 1)
      } else {
        hash.delete(el)
      }
    }
  })
  return res
//   // 双指针法
//   let p1 = 0
//   let p2 = 0
//   let res = []
//   nums1 = nums1.sort((a, b) => a - b)
//   nums2 = nums2.sort((a, b) => a - b)
//   while (p1 < nums1.length && p2 < nums2.length) {
//     if (nums1[p1] == nums2[p2]) {
//       res.push(nums1[p1])
//       p1++
//       p2++
//     } else if (nums1[p1] < nums2[p2]) {
//       p1++
//     } else {
//       p2++
//     }
//   }
//   return res
//   // 暴力法
// const res = []
// if (nums1.length < nums2.length) [nums1, nums2] = [nums2, nums1]
// for (let i = 0; i < nums1.length; i++) {
//   const key = nums2.indexOf(nums1[i])
//   if (key !== -1) res.push(nums2.splice(key, 1))
// }
// return res
}

Incoming [1, 2], [11, 1, 2, 3, 2]operating results

[ 1, 2 ]

Results of the

执行用时 :56 ms, 在所有 javascript 提交中击败了99.55% 的用户
内存消耗 :34.7 MB, 在所有 javascript 提交中击败了53.72%的用户

Guess you like

Origin www.cnblogs.com/moshuying/p/11828896.html