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(): 调用方 ...
随机推荐
- iOS 第三方开源库----->AFNetworking
AFNetworking AFNetworking是一个为 iOS 和 Mac OSX 制作的令人愉快的网络库,它建立在URL 装载系统框架的顶层,内置在Cocoa里,扩展了强有力的高级网络抽象.它 ...
- MVC3+中 ViewBag、ViewData和TempData的使用和区别
在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic),ViewData 是 ...
- sqlite与C++进行连接
SQLite数据库是零配置的,sqlite数据库不同于SqlServer等数据库,SQLite不需要复杂配置,只需要,将SQLite的库文件和动态链接文件拷贝到相应工程目录下,就可以使用SQLite数 ...
- JavaScript深复制
转载:http://blog.csdn.net/wanmingtom/article/details/7988284 这原本是StackOverFlow里的一个提问,看到答案后受益良多,于是翻译一下下 ...
- Python使用SMTP发送邮件[HTML格式、送带附件]
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. python的smtplib提供了一 ...
- 【Validate Binary Search Tree】cpp
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- Ubuntu 16.04 下卸载 lnmp/lamp 方法
1.卸载 apache2 sudo apt-get --purge remove apache2* sudo apt-get autoremove apache2 (--purge 是完全删除并且不保 ...
- WinForm Control - DataGridView
http://blog.csdn.net/fangxing80/article/details/1561011 .NET 2.0 - WinForm Control - DataGridView 编程 ...
- dancing link
http://www.cnblogs.com/grenet/p/3145800.html 链接给的博客写的很好,比较好懂. 可惜不是c语言... 于是决定自己要建一个模板. 一道裸题:hustoj 1 ...
- 【BZOJ】【1055】【HAOI2008】玩具取名
区间DP/记忆化搜索 sigh……看了提示才想到是区间DP >_>我果然还是太弱 f[l][r][k]表示L到R这段区间能否合并成K,那么就是枚举拆分方案(从哪里断开)和组合方式(左半合成 ...