public class Solution
{
public int[] Intersect(int[] nums1, int[] nums2)
{
var len1 = nums1.Length;
var len2 = nums2.Length; var list = new List<int>();//用于存放最后的结果 var dic1 = new Dictionary<int, int>();//记录第一个数组的特征
var dic2 = new Dictionary<int, int>();//记录第二个数组的特征 for (int i = ; i < len1; i++)
{
if (!dic1.ContainsKey(nums1[i]))
{
dic1.Add(nums1[i], );
}
else
{
dic1[nums1[i]]++;
}
} for (int i = ; i < len2; i++)
{
if (!dic2.ContainsKey(nums2[i]))
{
dic2.Add(nums2[i], );
}
else
{
dic2[nums2[i]]++;
}
} if (dic1.Count <= dic2.Count)
{
//以dic1为基础循环 foreach (var d in dic1)
{
if (dic2.ContainsKey(d.Key))
{
var count = Math.Min(d.Value, dic2[d.Key]);
while (count > )
{
list.Add(d.Key);
count--;
}
}
}
}
else
{
//以dic2为基础循环
foreach (var d in dic2)
{
if (dic1.ContainsKey(d.Key))
{
var count = Math.Min(d.Value, dic1[d.Key]);
while (count > )
{
list.Add(d.Key);
count--;
}
}
}
} foreach (var l in list)
{
Console.WriteLine(l);
} return list.ToArray();
}
}

https://leetcode.com/problems/intersection-of-two-arrays-ii/#/description

leetcode350的更多相关文章

  1. [Swift]LeetCode350. 两个数组的交集 II | Intersection of Two Arrays II

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  2. leetcode350之实现求解两数组交集(包含重复元素)

    给定两个数组,编写一个函数来计算它们的交集. 说明: 输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致. 我们可以不考虑输出结果的顺序 def binarySearch(nums, t ...

  3. [leetcode350]Intersection of Two Arrays II求数组交集

    List<Integer> res = new ArrayList<>(); Arrays.sort(nums1); Arrays.sort(nums2); int i1 = ...

  4. LeetCode 349,350 数组的交集

    LeetCode 349: package com.lt.datastructure.Set; import java.util.ArrayList; import java.util.LinkedH ...

随机推荐

  1. 【问题】解决在微信公众号里面网站无法访问:oops something went wrong:(

    最近在用一个第三方微信公众平台托管工具连接微信公众平台时,发现一个问题——在微信里面的官网网站链接没法在微信里面打开(无论是手机端还是PC端),会出现Oops! Something went wron ...

  2. LG3812 【模板】线性基

    题意 给定n个整数(数字可能重复),求在这些数中选取任意个,使得他们的异或和最大. \(1≤n≤50,0≤S_i≤2^{50}\) 分析 模板题. 推荐一篇好博客 现在我来证明一下线性基的性质. 性质 ...

  3. Django中提供了6种缓存方式

    开发调试 内存 文件 数据库 Memcache缓存(python-memcached模块) Memcache缓存(pylibmc模块) 1. 开发调试 1 2 3 4 5 6 7 8 9 10 11 ...

  4. oracle 查某一列有重复值的记录

    -- 查找重复记录select names,num from test where rowid != (select max(rowid)                  from test b   ...

  5. windows python3 安装gittle

    1 从github clone gittle项目, git clone https://github.com/FriendCode/gittle.git 2 进行到下载的项目,执行安装 python ...

  6. 【shell】sed命令

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed ...

  7. 解决eclipse中断点调试不起作用的问题

    解决eclipse中断点调试不起作用的问题   eclipsegeneration编译器file工作 最近几天,遇到了一个问题,就是在eclipse中进行断点调试程序到时候,跟踪不到我设置的断点.困惑 ...

  8. Mysql 性能优化2 系统参数配置方法 和 文件系统

    --------------------------------------------目录------------------------------------------------- • 关于 ...

  9. 【Spring学习笔记-MVC-11--】Spring MVC之表单标签

    一.使用方法 1.要使用Spring MVC提供的表单标签,首先需要在视图页面添加: <%@ taglib prefix="form" uri="http://ww ...

  10. 在VS2013平台下如何快速解决c++代码内存泄漏问题

    在学习FPS3000人脸关键点定位算法时,发现github上的源码,存在大量的内存泄漏问题,在训练的时发现内存一直在增长,测试的时候也存在内存无法彻底释放的问题. 一直以为是存放模型参数vector& ...