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?
如果给定的数组已经排序怎么办? 你会如何优化你的算法? 如果nums1的尺寸与nums2的尺寸相比较小怎么办? 哪种算法更好? 如果nums2的元素存储在磁盘上,并且内存有限,以致您无法一次将所有元素加载到内存中,该怎么办?
思路: 选择一个长度较小的数组,依次遍历,如果相同则提取出来。暴力法
网上思路: https://blog.csdn.net/coder_orz/article/details/51496278
出现错误:
IndentationError: unindent does not match any outer indentation level https://stackoverflow.com/questions/492387/indentationerror-unindent-does-not-match-any-outer-indentation-level