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. nexage video asset tag

    video  ad can't show InLine  must  match   the example ,and   xml content is  Case Sensitive https:/ ...

  2. Linux 性能工具安装部署

    docker 一.运行docker Linux内核版本需要在3.8以上,针对centos6.5 内核为2.6的系统需要先升级内核.不然会特别卡 在yum的ELRepo源中,有mainline(4.5) ...

  3. 罗技 M558 鼠标维修记录

    罗技 M558 鼠标维修记录 故障现象 按键不灵敏 拆机内部图 前进键 后退键 左键 右键 中键 自定义功能键 使用的是 OMRON 按键,好东西,质量可以. 但毕竟是机械的东西,还是有老化,用万用表 ...

  4. load-display-image之c#版

    基本功能 能够从文件load图像 -->显示图像-->在图像上方显示graphics,比如几条线-->鼠标移动,显示鼠标位置的灰度 load-display-image之c#版 lo ...

  5. mac系统下 Homebrew 使用

    brew 又叫 Homebrew,是一款Mac OS平台下的软件包管理工具. brew 常用命令: 命令 作用 brew install [package] 安装包 brew uninstall [p ...

  6. java 抽象类 以及模块方法设计模式,接口

    模块方法设计模式: 可以实现多接口 接口可以继承接口 接口与接口之间可以多继承.类与类之间不行

  7. Android 对话框(Dialog)大全

    转自: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html Activities提供了一种方便管理的创建.保存.回复的对话框机制, ...

  8. win7一键拖动生成快速启动栏

    一键拖动生成快速启动工具栏 ^#x:: ;自动添加快速启动工具栏 if had_added() ExitApp ql_add() Sleep, if (is_locked()) { lock_unlo ...

  9. 1110 Complete Binary Tree (25 分)

    1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...

  10. (转)linux查找技巧: find grep xargs

    在当前目录下所有.cpp文件中查找efg函数 find . -name "*.cpp" | xargs grep 'efg' xargs展开find获得的结果,使其作为grep的参 ...