题目

返回两个数组的交

样例

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

解题

排序后,两指针找相等元素,注意要去除相同的元素

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 tmpi = i;
int tmpj = j;
// 去重
while(i+1<nums1.length && nums1[i]==nums1[i+1]) i++;
while(j+1<nums2.length && nums2[j]==nums2[j+1]) j++;
// 没有重复,按照上面更新的i 和 j
if(tmpi<nums1.length && tmpi ==i){
i= tmpi;
}
if(tmpj<nums2.length && tmpj ==j){
j = tmpj;
}
}
int[] res = new int[A.size()];
for( i=0;i<A.size();i++){
res[i] = (int)A.get(i);
}
return res;
}
}

利用HashMap

将数组1的值唯一的保存在map中

根据map在去重

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);
}
}
ArrayList<Integer> A = new ArrayList<Integer>();
for(int i=0;i<nums2.length;i++){
if(map.containsKey(nums2[i])){ // 相同元素
A.add(nums2[i]);
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;
}
}

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

  1. [itint5]两有序数组的交和并

    这一题也简单,唯一有意思的地方是提炼了一个函数用来做数组索引去重前进. int forward(vector<int> &arr, int i) { while (i+1 < ...

  2. lintcode:两数组的交 II

    题目 计算两个数组的交 注意事项 每个元素出现次数得和在数组里一样答案可以以任意顺序给出 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2]. 解题 ...

  3. [Python] 比较两个数组的元素的异同

    通过set()获取两个数组的交/并/差集: print set(a) & set(b) # 交集, 等价于set(a).intersection(set(b)) print set(a) | ...

  4. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  5. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  6. [C++]for同时遍历两个数组

    C++11同时遍历两个数组 #define for2array(x,y,xArray,yArray) \ for(auto x=std::begin(xArray), x##_end=std::end ...

  7. PHP两个数组相加

    在PHP中,当两个数组相加时,会把第二个数组的取值添加到第一个数组上,同时覆盖掉下标相同的值: <?php $a = array("a" => "apple& ...

  8. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  9. array_intersect() php筛选两个数组共有的元素

    我们已经讲过如何筛选出连个数组中不共有的元素,今天就来看看php如何筛选出两个数组中共有的元素,例如筛选$array1和$array2共有的元素. 函数名:array_intersect(): 调用方 ...

随机推荐

  1. [LAMP]——mod_security和mod_evasive模块的安装

    系统版本:Red Hat 6 httpd版本:httpd-2.4.20 tar包:modsecurity-apache_2.5.9.tar.gz   mod_evasive_1.10.1.tar.gz ...

  2. SQL SERVER完整、差异和事务日志备份及还原(脚本和GUI实现) [原创]

    一.完整备份.差异备份和事务日志备份的脚本 --完整备份数据库 BACKUP DATABASE Test_Bak TO DISK = 'E:\20150609_75\bak\Test_bak_full ...

  3. 20145103《java程序设计》第4周学习总结

    20145103 <Java程序设计>第4周学习总结 教材学习内容总结 继承 继承共同行为 ·继承基本上就是避免多个类间重复定义共同行为. ·继承的三个好处:减少代码冗余:维护变得简单:扩 ...

  4. “我爱淘”冲刺阶段Scrum站立会议7

    完成任务: 大事不好,今天的任务还没有完成,没有通过xml文件通过服务器显示到软件中. 计划任务: 实现通过服务器将xml文件中的数据显示到软件中. 遇到问题: 服务器已经配好,并且解析xml文件的代 ...

  5. 四则运算出题器(C++)-BUG修复

    定制题目数量这个功能测试: (1)输入题目数为负数时: 可正确处理: (2)输入题目数量为0时: 可正确处理: (3)输入题目数量为小数时: 程序运行出错: 错误分析: 因为代码中题目数量的变量Que ...

  6. strlen和mb_strlen区别

    转自:http://blog.sina.com.cn/s/blog_5f0d5bd90100mzcl.html <?php//测试时文件的编码方式要是UTF8$str='中文a字1符';echo ...

  7. 【BZOJ】【1023】【SHOI2008】cactus仙人掌图

    DP+单调队列/仙人掌 题解:http://hzwer.com/4645.html->http://z55250825.blog.163.com/blog/static/150230809201 ...

  8. skinned mesh 蜘蛛样

    被skinned mesh 折磨了 好久,开始感觉skinindices不对,因为pix显示里面全是0 后来跟来跟去发现是这样的,那些uchar的整数被pix用float的格式显示出来 (显示为0.0 ...

  9. 利用WiFi钓鱼法追邻居漂亮妹纸

    假设,你的邻居是一个妹纸.漂亮单身,你,技术狗,家穷人丑,集体户口.像借酱油这种老套搭讪方式的成功率对你来说实在很低. 你要做的是了解她,然后接近她.通过搜集更多的情报,为创造机会提供帮助. 初级情报 ...

  10. JavaScript之setcookie()讲解

    function setcookie(name,value){          var Days = 30;          var exp  = new Date();          exp ...