题目

计算两个数组的交

注意事项

每个元素出现次数得和在数组里一样
答案可以以任意顺序给出

样例

nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].

解题

参考上道题,这道题只是不需要去重,直接把上道题去重部分程序去除就ok

public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
// Write your code here
Arrays.sort(nums1);
Arrays.sort(nums2);
ArrayList<Integer> A = new ArrayList<Integer>();
int i=0;
int j=0;
while(i<nums1.length && j<nums2.length ){
if(nums1[i] == nums2[j]){
A.add(nums1[i]);
i++;
j++; }else if(nums1[i] < nums2[j]){
i++;
}else{
j++;
} }
int[] res = new int[A.size()];
for( i=0;i<A.size();i++){
res[i] = (int)A.get(i);
}
return res;
}
}

HashMap记录数组1中相同元素出现的次数,数组2找相同,相同次数-1,为0的时候就不是交的部分了

这个HashMap是一个中间存储,方便找到相同元素

public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
// Write your code here
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i =0;i<nums1.length;i++){
if(!map.containsKey(nums1[i])){ // 唯一映射
map.put(nums1[i],1);
}else{
map.put(nums1[i],map.get(nums1[i])+ 1 );
}
}
ArrayList<Integer> A = new ArrayList<Integer>();
for(int i=0;i<nums2.length;i++){
if(map.containsKey(nums2[i])){ // 相同元素
A.add(nums2[i]);
map.put(nums2[i],map.get(nums2[i])- 1 );
if(map.get(nums2[i])==0)
map.remove(nums2[i]); // 去除相同元素
}
}
int[] res = new int[A.size()];
for(int i=0;i<A.size();i++){ // 保存到数组中
res[i] = A.get(i);
}
return res;
}
}

更新

public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
// Write your code here
if(nums1==null || nums2 ==null)
return new int[]{};
HashMap<Integer,int[]> map = new HashMap<Integer,int[]>();
List<Integer> list = new ArrayList<Integer>();
for(int i=0;i<nums1.length;i++){
int[] value = map.get(nums1[i]);
if(value == null){
map.put(nums1[i],new int[]{1});
}else{
value[0]++;
}
}
for(int i=0;i<nums2.length;i++){
int[] value = map.get(nums2[i]);
if(value!=null && value[0]>=1){
list.add(nums2[i]);
value[0]--;
}
}
int[] A = new int[list.size()];
for(int i=0;i<list.size();i++){
A[i] = list.get(i);
}
return A;
}
}

自定义value

public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
// Write your code here
if(nums1==null || nums2 ==null)
return new int[]{};
HashMap<Integer,Value> map = new HashMap<Integer,Value>();
List<Integer> list = new ArrayList<Integer>();
for(int i=0;i<nums1.length;i++){
Value value = map.get(nums1[i]); if(value == null){
map.put(nums1[i],new Value(1));
}else{
int v = value.getValue();
value.setValue(v+1);
}
}
for(int i=0;i<nums2.length;i++){
Value value = map.get(nums2[i]);
if(value!=null){
int v = value.getValue();
if(v>=1){
list.add(nums2[i]);
value.setValue(v-1);
} }
}
int[] A = new int[list.size()];
for(int i=0;i<list.size();i++){
A[i] = list.get(i);
}
return A;
}
public class Value{
int i;
Value(int i){
this.i = i;
}
public void setValue(int i){
this.i = i;
}
public int getValue(){
return i;
}
}
}

lintcode:两数组的交 II的更多相关文章

  1. lintcode:两个数组的交

    题目 返回两个数组的交 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2]. 解题 排序后,两指针找相等元素,注意要去除相同的元素 public class ...

  2. 6、两个数组的交集 II

    6.两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: n ...

  3. leecode刷题(6)-- 两个数组的交集II

    leecode刷题(6)-- 两个数组的交集II 两个数组的交集II 描述: 给定两个数组,编写一个函数来计算它们的交集. 示例: 输入: nums1 = [1,2,2,1], nums2 = [2, ...

  4. 【Leetcode】【简单】【350. 两个数组的交集 II】【JavaScript】

    题目描述 350. 两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2]输出: [2,2] 示例 2 ...

  5. 前端与算法 leetcode 350. 两个数组的交集 II

    目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...

  6. Java实现 LeetCode 350 两个数组的交集 II(二)

    350. 两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入 ...

  7. LeetCode初级算法之数组:350 两个数组的交集 II

    两个数组的交集 II 题目地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ 给定两个数组,编写一个函数来计算它们的交 ...

  8. Java实现 LeetCode 462 最少移动次数使数组元素相等 II

    462. 最少移动次数使数组元素相等 II 给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000. 例如: 输 ...

  9. js 两数组去除重复数值

    //两数组去除重复数值 mergeArray: function(arr1, arr2) { for (var i = 0; i < arr1.length; i++) { for (var j ...

随机推荐

  1. 在windows上使用symfony创建简易的CMS系统(一)

    http://blog.csdn.net/kunshan_shenbin/article/details/7164675 参考自:http://xsymfony.801.cxne.net/forum. ...

  2. licens 问题 Error (292028): Specified license is not valid for this machine

    集成网卡调试的时候坏了,造成了quartus 不可以用,MAC地址不对应了... 应该怎么解决呢??.

  3. C++函数的参数传递机制以及参数的类型选择

    C++primer之函数的参数传递以及参数的类型 一:函数的基本知识 (1)      函数要素:返回类型,函数名字,形参(参数之间用逗号隔开) (2)      函数调用机制:我们通过调用运算符来执 ...

  4. c读取文本文档

    想数一下文本文档一共有多少行,写了个小程序 1.用fopen()以只读方式打开文件 2.用fgetc()获取文件流中的字符内容 3.如果字符内容为'\n'换行符,count++ 最后输出count的值 ...

  5. Google Volley: How to send a POST request with Json data?

    sonObjectRequest actuallyaccepts JSONObject as body. From http://arnab.ch/blog/2013/08/asynchronous- ...

  6. bower解决js的依赖管理

    bower解决js的依赖管理 前言: 一个新的web项目开始,我们总是很自然地去下载需要用到的js类库文件,比如jQuery,去官网下载名为jquery-1.10.2.min.js文件,放到我们的项目 ...

  7. 一些常用到的Centos命令

    CentOS常用命令在我们的使用中,经常被使用.所以,我们对一些经常使用又很重要的CentOS常用命令进行了全面的整理.下面,就来介绍这些CentOS常用命令. 一:使用CentOS常用命令查看cpu ...

  8. mac下phpstorm左侧的project列表找不到了

    早上开机,发现左侧的project列表没有了,用着不方便,当然了,是可以调出来的. View-Tool Windows-Project,就出来来. 快捷键:command+1. 问题解决.

  9. python 循环技巧

    原文地址:http://docs.pythontab.com/python/python3.4/datastructures.html#tut-tuples 在字典中循环时,关键字和对应的值可以使用  ...

  10. nodeJs 初探 ~

    今天晚上,开始时间了一下nodejs,跟着 Node入门 一步步的往下走.对node开发也有了初步的了解. 期间没有碰到什么问题,只有在最后的时候,碰到了几个问题.在这里记录一下: 1 . cross ...