首页 > 编程语言 > 详细

349.求两个数组的交集 Intersection of Two Arrays

时间:2017-01-10 23:41:37      阅读:297      评论:0      收藏:0      [点我收藏+]

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

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

Note:

  • Each element in the result must be unique.
  • The result can be in any order.

Subscribe to see which companies asked this question

  1. public int[] Intersection(int[] nums1, int[] nums2) {
  2. List<int> l = new List<int>();
  3. List<int> sameList = new List<int>();
  4. int length = nums1.Length;
  5. for (int i = 0; i < length; i++) {
  6. l.Add(nums1[i]);
  7. }
  8. length = nums2.Length;
  9. int num = 0;
  10. for (int i = 0; i < length; i++) {
  11. num = nums2[i];
  12. if (l.Contains(num) && !sameList.Contains(num)) {
  13. sameList.Add(num);
  14. }
  15. }
  16. int [] list = sameList.ToArray();
  17. return list;
  18. }





349.求两个数组的交集 Intersection of Two Arrays

原文:http://www.cnblogs.com/xiejunzhao/p/be76b2bc9eee928c4debbb5933430f65.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!