码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-01-10 23:44:16      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:one   .com   ati   交集   span   rgb   each   style   pre   

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

标签:one   .com   ati   交集   span   rgb   each   style   pre   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!