题目

计算两个数组的交

注意事项

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

样例

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. iOS 进阶 第十六天(0419)

    0419 任何view默认不支持多点触控,有一个属性设置Multiple Touch,设置为Yes即可支持多点触控 触摸移动一个view,让view也跟着动代码 关于触摸的一些解释: 注意:touch ...

  2. PBOC2.0与PBOC3.0的区别

    2013年2月,中国人民银行发布了<中国金融集成电路(IC)卡规范(V3.0)>(以下简称PBOC3.0),PBOC3.0是在中国人民银行2005年颁布的<中国金融集成电路(IC)卡 ...

  3. C++中的运算符重载注意事项

    1.C++中的运算符重载的方式有三种: a.类成员函数重载 b.友元函数重载 c.普通函数重载 注意: a.我们主要使用的方式主要是用:类成员函数和友元函数来实现运算符的重载. b.其实用普通函数理论 ...

  4. js之内置对象

    内置对象(Global和Math):JS程序在执行之前就已经存在,开发人员不必再取实例化的内置对象 下面对Global对象进行介绍一下,Math用的不多就不做介绍了 1.Global对象 Global ...

  5. 【Maximum Depth of Binary Tree 】cpp

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  6. eclipse部署Tomcat6 : The server does not support version 3.0 of the JEE Web module specification

    为项目添加tomcat 6,发现不能添加,原因如下 这是因为Tomcat6不能为JavaEE3.0版本服务,把项目的版本降低到2.5就可以了 现在可以部署了

  7. zoj 2314 Reactor Cooling 网络流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 The terrorist group leaded by a ...

  8. lua与 object-C 通信

    IOS中如何调用LUA,以及LUA如何调用IOS中的功能 下面将讲解一下如何在iOS里调用Lua函数,以及Lua函数如何调用iOS本地函数. 转载请注明出处.原文出处 http://www.cnblo ...

  9. [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

    http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...

  10. Nginx的accept_mutex配置分析

    让我们看看accept_mutex的意义:当一个新连接到达时,如果激活了accept_mutex,那么多个Worker将以串行方式来处理,其中有一个Worker会被唤醒,其他的Worker继续保持休眠 ...