题目

返回两个数组的交

样例

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. ios 将Log日志重定向输出到文件中保存

    对于真机,日志没法保存,不好分析问题.所以有必要将日志保存到应用的Docunment目录下,并设置成共享文件,这样才能取出分析. 首先是日志输出,分为c的printf和标准的NSLog输出,print ...

  2. EF简单的增删查改

    Add /// <summary> /// /// </summary> public void Add() { TestDBEntities2 testdb = new Te ...

  3. 【转】matlab采样函数

    dyaddown 功能:对时间序列进行二元采样,每隔一个元素提取一个元素,得到一个降采样时间序列. 格式: 1.y = dyaddown(x, EVENODD) 当EVENODD=0时,从x中第二个元 ...

  4. python中fork()函数生成子进程分析

    python的os module中有fork()函数用于生成子进程,生成的子进程是父进程的镜像,但是它们有各自的地址空间,子进程复制一份父进程内存给自己,两个进程之 间的执行是相互独立的,其执行顺序可 ...

  5. mysql开机脚本

    #!/bin/bash basedir=/home/app/db/mysql datadir=$basedir/data conf=$basedir/etc/my.cnf pid_file=$data ...

  6. xcode 使用通用总结

    一.搜索东西 有时候类很多,方法很多,想改某类时,还要打开各自文件夹去点进去,感觉很麻烦费时间. 如下图:用此搜索可以搜到此类,从而进行修改. 有时候想在类中找某个方法或者属性,自已一点一点找很费劲, ...

  7. js判断浏览器滚动条是否拉到底

    $(window).scroll(function(){ // 当滚动到最底部以上n像素时, 加载新内容 if ($(document).height() - $(this).scrollTop() ...

  8. Careercup - Google面试题 - 4877486110277632

    2014-05-08 05:16 题目链接 原题: Given a circle with N defined points and a point M outside the circle, fin ...

  9. 20、android解决方案(转载)

    目录: 1.广告 2.推送 3.云 4.统计 5.后端存储 6.地图 7.测试 8.托管 9.支付 10.音视频 11.社会化分享 12.存储 13.自动更新 14.轻开发 15.安全 16.图像 1 ...

  10. [转载]char * 和char []的区别---之第一篇

    char *  和char []的区别---之第一篇 原文地址http://blog.csdn.net/yahohi/article/details/7427724 在C/C++中,指针和数组在很多地 ...