首页 > Windows开发 > 详细

LeetCode-C#实现-哈希表(#349)

时间:2018-12-10 12:42:42      阅读:183      评论:0      收藏:0      [点我收藏+]

349. Intersection of Two Arrays

两个数组的交集

public class Solution {
    public int[] Intersection(int[] nums1, int[] nums2) {
        //使用哈希集合去重
        HashSet<int> hashSet1=new HashSet<int>(nums1);
        HashSet<int> hashSet2=new HashSet<int>(nums2);
        List<int> list=new List<int>();
        //遍历两哈希集合,相等者加入集合,遍历结束后将集合转为数组返回
        foreach(int h1 in hashSet1){
            foreach(int h2 in hashSet2){
                if(h1==h2)list.Add(h1);
            }
        }
        return list.ToArray<int>();
    }
}

 

LeetCode-C#实现-哈希表(#349)

原文:https://www.cnblogs.com/errornull/p/10095741.html

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