LeetCode编程练习 - Intersection of Two Arrays II学习心得

题目:

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

     Example:
     Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return[2, 2].

    Note:

      1.Each element in the result should appear as many times as it shows in both arrays.

      2.The result can be in any order.

    Follow up:

      1.What if the given array is already sorted? How would you optimize your algorithm?

      2.What if nums1's size is small compared to nums2's size? Which algorithm is better?

      3.What if elements ofnums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

      给定两个数组,输出它们的交集。


思路:

      对两个数组进行排序,然后遍历两个数组进行比较,若相等,则将结果保存到a中,然后将两个索引递加,若不相等,将较小的数组的索引递加。Count()时表示元素的个数,用于由多个对象组成的类,比如说集合List,Hash Table,而Length是指长度,是作为一个整体看待,一般面向对象String。

     可以参照Intersection of Two Arrays

LeetCode编程练习 - Intersection of Two Arrays II学习心得