Given two arrays, write a function to compute their intersection.
Example 1:
1 | Input: nums1 = [1,2,2,1], nums2 = [2,2] |
Example 2:
1 | Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] |
Note:
1 | Each element in the result should appear as many times as it shows in both arrays. |
這題要找兩list交集,第一個想法可能會用2個for尋訪,那會得到O(nˆ2)效率不佳,而python跟javaㄧ樣有collection,其中的Counter(list)可以算list元素個數給出dict,再用&取交集,最後記得加.elements(),不然只會有單一元素。
如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)