zoukankan      html  css  js  c++  java
  • LeetCode_349.两个数组的交集

    给定两个数组,编写一个函数来计算它们的交集。

    示例 1:

    输入:nums1 = [1,2,2,1], nums2 = [2,2]
    输出:[2]
    

    示例 2:

    输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
    输出:[9,4]

    说明:

    • 输出结果中的每个元素一定是唯一的。
    • 我们可以不考虑输出结果的顺序。

    C#代码

    public class Solution {
        public int[] Intersection(int[] nums1, int[] nums2) {
            Dictionary<int, int> dic = new Dictionary<int, int>();
            foreach (var item in nums1)
            {
                if (dic.ContainsKey(item) == false)
                {
                    dic.Add(item, item);
                }
            }
    
            List<int> list = new List<int>();
            foreach (var item in nums2)
            {
                if (dic.ContainsKey(item))
                {
                    list.Add(item);
                    dic.Remove(item);
                }
            }
    
            int[] array = list.ToArray();
            return array;
        }
    }
    
  • 相关阅读:
    合一算法最新版
    string.at(i)
    字符串逆转
    String
    Vector
    1005POJ
    但愿天堂一切都好
    合一算法
    合一算法2
    BTREE与其它索引的优缺点对比
  • 原文地址:https://www.cnblogs.com/fuxuyang/p/14244630.html
Copyright © 2011-2022 走看看