/ * *

350. Intersection of two arrays II

The title

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

Example 1:

Input: nums1 =,2,2,1 [1], nums2 = (2, 2] output: [2] example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

Description:

The number of occurrences of each element in the output should be the same as the minimum number of occurrences of the element in both arrays. We can ignore the order of the output. Advanced:

What if the given array is already sorted? How will you optimize your algorithm? If nums1 is much smaller than NUMs2, which method is better? What do you do if nums2 elements are stored on disk, memory is limited, and you can’t load all elements into memory at once?

Source: LeetCode link: leetcode-cn.com/problems/in… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

The test code

Print (intersects (,9,5 [4], [9,4,9,8,4]))

notes

So this is going to use the hash table to find out if the elements of o(1) are keys and values

Note that it’s faster to store a small array of data in a hash table and then match it with a large array

The code address

Github.com/zmfflying/Z… * /

The problem solving code

import Foundation func intersect(_ nums1: [Int], _ nums2: [Int]) -> [Int] {if nums1.count > nums2.count {return intersect(nums2, nums1)} var res: [Int] = [Int]() var dic: [Int: Int] = [Int: Value for num in nums1 {dic[num] = dic[num] == nil? 1 : (dic[num]! For num in nums2 {if dic[num]! = nil { res.append(num) let count = dic[num]! - 1 dic[num] = count == 0 ? If res.count == nums1.count {break}} return res}Copy the code

/ * *

389. To find different

The title

Given two strings s and t, they contain only lowercase letters.

The string t is randomly rearranged by the string S, and a letter is added at the random position.

Please find the letter added to t.

Example 1:

Input: s = “abcd”, t = “abcde” Output: “e” Explanation: ‘e’ is the letter being added. Example 2:

Input: s = “”, t = “y” Output: “y” Example 3:

Input: s = “a”, t = “aa” Output: “a” Example 4:

Input: s = “ae”, t = “aea”

Tip:

0 <= s.length <= 1000 t.length == S.Length + 1 S and T contain only lowercase letters

Source: LeetCode link: leetcode-cn.com/problems/fi… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

The test code

print(findTheDifference(“ae”, “aea”))

notes

This problem is faster in ASCII

You can also use the hash table lookup o(1) feature to create keys and values

The code address

Github.com/zmfflying/Z… * /

The problem solving code

import Foundation func findTheDifference(_ s: String, _ t: String) -> Character { if s.count == 0 { return Character.init(t) } var dic: [Character: Int] = [Character: Int]() for c in Array(s) { dic[c] = dic[c] == nil ? 1 : (dic[c]! + 1) } for c in Array(t) { if dic[c] ! = nil { let count = dic[c]! - 1 dic[c] = count == 0 ? Nil: count} else {return c}} return "a"} //func findTheDifference(_ s: String, _ t: String) -> Character { // var sum1: UInt32 = 0, sum2: UInt32 = 0 // for v in s.unicodeScalars { // sum1 += v.value // } // for v in t.unicodeScalars { // sum2 += v.value // }  // // return Character(Unicode.Scalar(sum2 - sum1)!) / /}Copy the code