lintcode:两数组的交 II
题目
计算两个数组的交
注意事项
每个元素出现次数得和在数组里一样
答案可以以任意顺序给出
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的更多相关文章
- lintcode:两个数组的交
题目 返回两个数组的交 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2]. 解题 排序后,两指针找相等元素,注意要去除相同的元素 public class ...
- 6、两个数组的交集 II
6.两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: n ...
- leecode刷题(6)-- 两个数组的交集II
leecode刷题(6)-- 两个数组的交集II 两个数组的交集II 描述: 给定两个数组,编写一个函数来计算它们的交集. 示例: 输入: nums1 = [1,2,2,1], nums2 = [2, ...
- 【Leetcode】【简单】【350. 两个数组的交集 II】【JavaScript】
题目描述 350. 两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2]输出: [2,2] 示例 2 ...
- 前端与算法 leetcode 350. 两个数组的交集 II
目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...
- Java实现 LeetCode 350 两个数组的交集 II(二)
350. 两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入 ...
- LeetCode初级算法之数组:350 两个数组的交集 II
两个数组的交集 II 题目地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ 给定两个数组,编写一个函数来计算它们的交 ...
- Java实现 LeetCode 462 最少移动次数使数组元素相等 II
462. 最少移动次数使数组元素相等 II 给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000. 例如: 输 ...
- js 两数组去除重复数值
//两数组去除重复数值 mergeArray: function(arr1, arr2) { for (var i = 0; i < arr1.length; i++) { for (var j ...
随机推荐
- sharepoint 2010 重建遇到的问题
需要重新安装Sharepoint 2010 ,遇到问题记录下来,sharepoint中安装了热补丁和英文语言包. 卸载: 1.运行配置向导将服务器从服务场中脱离: 2.在管理中心中卸载sharepoi ...
- perl DBI 学习总结(转载)
perl DBI 学习总结 源文地址:http://blog.csdn.net/like_zhz/article/details/5441946 DBI和DBD的不同关系模型: ########### ...
- POC - ASP.NET & IIS 部分
终于得到了我VM的管理员权限啦啦.接下来不需要把IIS架在我自己的电脑上了,将架在我的VM上. 1. 先添加ISAP和CGI的组件. 2. 将defaultAppPool的MODE设为CLASSIC, ...
- EasyUI datagrid frozencolumn的bug???
今天碰到了个很蛋疼的问题.我用到了easyui 的 treegrid,内容只显示一列,我把它设置成了冻结列. 在谷歌调试下,因为内容比较多,所以,会有竖向的滚动条.但是,到了ie和火狐,滚动条神奇般没 ...
- How to compile and install NCAR Command Language on IBM PowerPC 64 --- NCL编译安装步骤
作者:Sinsonglew 出处:http://www.cnblogs.com/sinsonglew 欢迎转载,也请保留这段声明.thanks :) 注记:NCL官方依赖安装包全集列表.官方源码编译指 ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- 利用Shell命令获取IP地址
一 :获取单个网卡的IPv4地址,方法如下: 方法一:$/sbin/ifconfig ethX | awk '/inet addr/ {print $2}' | cut -f2 -d ":& ...
- android开发实现静默安装(fota升级)
这里只提供一个思路,也是咨询大神才了解到的. fota升级主要用于系统及系统应用的升级,不过貌似也会弹出提示用于用户确认.既然做到系统级别了,估计也一样可以静默安装的.
- ZBar之自定义二维码扫描
// // YvanQRCodeViewController.m // zBar // // Created by City--Online on 15/6/8. // Copyright (c) 2 ...
- Incorrect string value: '\xF0\xA1\xA1\x92' for column 'herst' at row 1
Incorrect string value: '\xF0\xA1\xA1\x92' for column 'herst' at row 1[转] 1.一般来说MySQL(小于5.5.3)字符集设置为 ...