[itint5]两有序数组的交和并
这一题也简单,唯一有意思的地方是提炼了一个函数用来做数组索引去重前进。
int forward(vector<int> &arr, int i) {
while (i+1 < arr.size() && arr[i] == arr[i+1]) i++;
i++;
return i;
} vector<int> arrayUnion(vector<int> &a, vector<int> &b) {
vector<int> ans;
int i = 0;
int j = 0;
while (i < a.size() && j < b.size()) {
if (a[i] == b[j]) {
ans.push_back(a[i]);
i = forward(a, i);
j = forward(b, j);
} else if (a[i] < b[j]) {
ans.push_back(a[i]);
i = forward(a, i);
} else {
ans.push_back(b[j]);
j = forward(b, j);
}
}
while (i < a.size()) {
ans.push_back(a[i]);
i = forward(a, i);
}
while (j < b.size()) {
ans.push_back(b[j]);
j = forward(b, j);
}
return ans;
} vector<int> arrayIntersect(vector<int> &a, vector<int> &b) {
vector<int> ans;
int i = 0;
int j = 0;
while (i < a.size() && j < b.size()) {
if (a[i] == b[j]) {
ans.push_back(a[i]);
i = forward(a, i);
j = forward(b, j);
} else if (a[i] < b[j]) {
i = forward(a, i);
} else {
j = forward(b, j);
}
}
return ans;
}
[itint5]两有序数组的交和并的更多相关文章
- [itint5]两有序数组的中位数
这个题和leetcode的基本一样.用了更好点的思路.在A中折半猜是不是中位数,A中没有后在B中猜.最后猜到B[j]<=A[i]<=B[j+1],此时,无论奇偶(2k+1或者2k个),A[ ...
- lintcode:两个数组的交
题目 返回两个数组的交 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2]. 解题 排序后,两指针找相等元素,注意要去除相同的元素 public class ...
- lintcode:两数组的交 II
题目 计算两个数组的交 注意事项 每个元素出现次数得和在数组里一样答案可以以任意顺序给出 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2]. 解题 ...
- 两个有序数组的上中位数和第K小数问题
哈,再介绍个操蛋的问题.当然,网上有很多解答,但是能让你完全看懂的不多,即便它的结果是正确的,可是解释上也是有问题的. 所以,为了以示正听,我也做了分析和demo,只要你愿意学习,你就一定能学会,并且 ...
- 计算两个有序数组的第K大数(转)
传统解法,最直观的解法是O(m+n).直接merge两个数组,然后求第K大的数字. 如果想要时间复杂度将为O(log(m+n)).我们可以考虑从K入手.如果我们每次能够删除一个一定在第K个元素之前的元 ...
- 3299 有序数组合并求第K大问题
题目描述 Description 给出两个有序数组A和B(从小到大有序),合并两个有序数组后新数组c也有序,询问c数组中第k大的数 假设不计入输入输出复杂度,你能否给出一个O(logN)的方法? 输入 ...
- [Python] 比较两个数组的元素的异同
通过set()获取两个数组的交/并/差集: print set(a) & set(b) # 交集, 等价于set(a).intersection(set(b)) print set(a) | ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
随机推荐
- (转)设置Win7防火墙规则 顺畅访问局域网
在Windows 7系统的电脑上搭建WAMP环境后,发现在局域网中其他电脑不能访问.有朋友告诉小强,这可能是因为当时Windows 7自带的防火墙屏蔽了80端口,只需要重新设置规则就可以了. 点击Wi ...
- 【转帖】error C2296: “^”: 非法,左操作数包含“double”类型
想要实现 ,写的C++程序为 double x; x=2^3; 结果程序总是出现这样的错误:error C2296: “^”: 非法,左操作数包含“double”类型 后来才发现操作符“^”,在C++ ...
- jQuery ajax 请求php遍历json数组到table中
html代码(test.html),js在html底部 <!DOCTYPE html> <html lang="en"> <head> < ...
- mysql table readonly
if the table does not have primary key or unique non-nullable defined, then MySql workbench could no ...
- swing画太极图案源码
package org.open.swing.taiji; /** * @(#)Taichi.java * * * @author * @version 1.00 2007/6/12 */ impor ...
- javascript 事件的学习
1.事件绑定: addEventListener , removeEventListener.是dom2级别的事件绑定 attachEvent , detachEvent 是IE的事件绑定. 2. 事 ...
- uva401 - Palindromes结题报告
题目地址 : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- web.xml常用元素
web.xml文件是用来初始化配置信息:比如welcome页面.servlet.servlet-mapping.filter.listener.启动加载级别等.当你的web工程没用到这些时,你可以不用 ...
- shell调用sqlplus批量执行sql文件
在最近的工作中,经常需要批量执行一些DML, DDL, PL/SQL语句或导入一些Function, Procedure.因为support的国家比较多,常常需要一个登陆到一个国家的数据库上执行完成后 ...
- 【BZOJ 1791】 [Ioi2008]Island 岛屿
Description 你将要游览一个有N个岛屿的公园.从每一个岛i出发,只建造一座桥.桥的长度以Li表示.公园内总共有N座桥.尽管每座桥由一个岛连到另一个岛,但每座桥均可以双向行走.同时,每一对这样 ...