lintcode:两个数组的交
题目
返回两个数组的交
样例
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:两个数组的交的更多相关文章
- [itint5]两有序数组的交和并
这一题也简单,唯一有意思的地方是提炼了一个函数用来做数组索引去重前进. int forward(vector<int> &arr, int i) { while (i+1 < ...
- lintcode:两数组的交 II
题目 计算两个数组的交 注意事项 每个元素出现次数得和在数组里一样答案可以以任意顺序给出 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2]. 解题 ...
- [Python] 比较两个数组的元素的异同
通过set()获取两个数组的交/并/差集: print set(a) & set(b) # 交集, 等价于set(a).intersection(set(b)) print set(a) | ...
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [C++]for同时遍历两个数组
C++11同时遍历两个数组 #define for2array(x,y,xArray,yArray) \ for(auto x=std::begin(xArray), x##_end=std::end ...
- PHP两个数组相加
在PHP中,当两个数组相加时,会把第二个数组的取值添加到第一个数组上,同时覆盖掉下标相同的值: <?php $a = array("a" => "apple& ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- array_intersect() php筛选两个数组共有的元素
我们已经讲过如何筛选出连个数组中不共有的元素,今天就来看看php如何筛选出两个数组中共有的元素,例如筛选$array1和$array2共有的元素. 函数名:array_intersect(): 调用方 ...
随机推荐
- ifstat-网络接口监测工具
ifstat-网络接口监测工具 http://gael.roualland.free.fr/ifstat/ ifstat is a tool to report network interfaces ...
- [无人值守安装操作系统]__RHEL6__FTP+TFTP+DHCP+Kickstart+PXE
实验环境 1.实验平台:VMware Workstation 10 2.实验OS:RHEL6 3.服务器A: (1) 10.0.10.158 (2) DHCP/FTP/TFTP (3) 有可使用的yu ...
- hadoop日志太大
hadoop jobtracker日志太大在jobtracker服务器上的mapred-site.xml中添加以下参数: <property> <name>mapreduce. ...
- Mysql高级之存储过程
参考地址1:http://www.2cto.com/database/201411/350819.html 参考地址2:http://www.jb51.net/article/39471.htm my ...
- sql中having的使用
where 和having有什么区别? where 是group by之前进行筛选,having是group by 之后进行统计的筛选,一般having会和group by一起使用,结合聚合函数,统计 ...
- UVALive - 6955 Finding Lines 随机算法
题目链接: http://acm.hust.edu.cn/vjudge/contest/126968#problem/F 题意 给你n个点,问是否有>=p/100*n个点共线(p>=20& ...
- hdu 1043 Eight 经典八数码问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...
- VS开发工具 不会在异常的地方停止的问题.
启用"仅我的代码"
- Javascript动态生成表格的性能调优
vision 0.8 [耗时672ms]终极优化 将字符串作为数组对象的方式是目前效率最高,性能最优的方式. <script> var t1 = new Date(); < ...
- c# 简单文件流读写CSV文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...