leetcode350
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的更多相关文章
- [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 ...
- leetcode350之实现求解两数组交集(包含重复元素)
给定两个数组,编写一个函数来计算它们的交集. 说明: 输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致. 我们可以不考虑输出结果的顺序 def binarySearch(nums, t ...
- [leetcode350]Intersection of Two Arrays II求数组交集
List<Integer> res = new ArrayList<>(); Arrays.sort(nums1); Arrays.sort(nums2); int i1 = ...
- LeetCode 349,350 数组的交集
LeetCode 349: package com.lt.datastructure.Set; import java.util.ArrayList; import java.util.LinkedH ...
随机推荐
- LeetCode-Microsoft-Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- jQuery插件制作方法详解
jQuery插件制作方法详解 jquery插件给我的感觉清一色的清洁,简单.如Jtip,要使用它的功能,只需要在你的元素的class上加 上Jtip,并引入jtip.js及其样式即可以了. ...
- 比jsonpath 更方便的json 数据查询JMESPath 使用
类似xml 的xpath json 有jsonpath 都是为了方便进行数据查询,但是jsonpath 的功能 并不是很强大,如果为了方便查询可以使用jmespath. 以下为简单使用: 查询格式 ...
- 对象的继承(__proto__和Object.setPrototypeOf(child,father))
两个对象间的继承
- FastAdmin composer json 版本说明
来源于 FastAdmin 执行 composer update 后将 ThinkPHP 升级到了 V5.1. FastAdmin 是基于 ThinkPHP 5.0.x 开发的,而 ThinkPHP ...
- 【转】RS232、RS485、TTL电平、CMOS电平
原文网址:http://blog.sina.com.cn/s/blog_63a0638101018grc.html RS232.RS485.TTL电平.CMOS电平 什么是TTL电平.CMOS电平.R ...
- Oracle DataBase单实例使用ASM案例(1)--ASM基本概念
版权声明:本文为博主原创文章,未经博主允许不得转载. Oracle DataBase单实例使用ASM案例(1)--ASM基本概念 系统环境: 操作系统:RH EL5-64 Oracle 软件: Ora ...
- 嵌入式QT程序的汉字显示
因底层服务程序全是GBK格式的,所以QT程序要全部更改编码方式. 1.QT程序编码更改 creator->edit->更改编码方式GBK main程序中做如下修改,并注意语句次序 int ...
- 【Hibernate学习笔记-4】在hibernate.cfg.xml中配置C3P0数据源
jar包 hibernate.cfg.xml <?xml version="1.0" encoding="GBK"?> <!DOCTYPE h ...
- 【转】asp.net mvc css/js压缩合并 --- combres
转自:http://www.cnblogs.com/zxktxj/archive/2012/05/30/2526246.html NuGet 网站:http://nuget.codeplex.co ...