Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

350. Intersection of Two Arrays II

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

Example 1:

1
2
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:

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

Note:

1
2
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.

這題要找兩list交集,第一個想法可能會用2個for尋訪,那會得到O(nˆ2)效率不佳,而python跟javaㄧ樣有collection,其中的Counter(list)可以算list元素個數給出dict,再用&取交集,最後記得加.elements(),不然只會有單一元素。


如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels