Union and Intersection of two sorted lists 并集和交集

跟面试官确认是arrayList还是singly-linked list
/*
Union 并集:两个升序的list a, b, 返回其并集(升序排序)
*/
public class UnionTwoSortedLists {
public List<Integer> union(List<Integer> a, List<Integer> b) {
List<Integer> result = new ArrayList<>();
// Use two index variables i and j, initial values i = 0, j = 0
int i = 0, j = 0;
while (i < a.size() && j < b.size()) {
// If a.get(i) < b.get(j) then add a.get(i) to result and increment i.
if (a.get(i) < b.get(j)) {
result.add(a.get(i));
i++;
}
// If b.get(j) < a.get(i) then add b.get(j) to result and increment j.
else if (b.get(j) < a.get(i)) {
result.add(b.get(j));
j++;
}
// If both are same then add any to result and increment both i and j.
else {
result.add(b.get(j));
i++;
j++;
}
}
/* handle the case a.size()!= b.size(), add remaining elements of the larger array */
while (i < a.size()){
result.add(a.get(i));
i++;
}
while (j < b.size()){
result.add(b.get(j));
j++;
}
return result;
}
public static void main(String[] args) {
List<Integer> l1 = new ArrayList<>();
l1.add(1);
l1.add(2);
l1.add(3);
List<Integer> l2 = new ArrayList<>();
l2.add(2);
l2.add(3);
UnionTwoSortedLists test = new UnionTwoSortedLists();
for(int n : test.union(l1,l2)){
System.out.println(n);
}
}
}
/*
Intersection 交集:两个升序的list a, b, 返回其交集(升序排序)
*/
public class IntersectionTwoSortedLists {
public List<Integer> intersection(List<Integer> a, List<Integer> b) {
List<Integer> result = new ArrayList<>();
// Use two index variables i and j, initial values i = 0, j = 0
int i = 0, j = 0;
while (i < a.size() && j < b.size()) {
// If a.get(i) < b.get(j) then increment i.
if (a.get(i) < b.get(j)) {
i++;
}
// If b.get(j) < a.get(i) then increment j.
else if (b.get(j) < a.get(i)) {
j++;
}
// If both are same then add any to result and increment both i and j.
else {
result.add(b.get(j));
i++;
j++;
}
}
return result;
}
public static void main(String args[]) {
List<Integer> l1 = new ArrayList<>();
l1.add(1);
l1.add(2);
l1.add(3);
List<Integer> l2 = new ArrayList<>();
l2.add(2);
l2.add(5);
IntersectionTwoSortedLists test = new IntersectionTwoSortedLists();
for (int n : test.intersection(l1, l2)) {
System.out.println(n);
}
}
}
followup1: 上述问题是2个list, 求问n个list的union和intersection
Union and Intersection of two sorted lists 并集和交集的更多相关文章
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [LeetCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [LintCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...
- No.023:Merge k Sorted Lists
问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- Merge k Sorted Lists
1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...
- 71. Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Merge Two Sorted Lists
Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...
随机推荐
- LeetCode 题解 Search a 2D Matrix II。巧妙!
[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30 ...
- python中序列化json模块和pickle模块
内置模块和第三方模块 json模块和pickle 模块(序列化模块) 什么是序列化? 序列化就是将内粗这种的数据类型转成另一种格式 序列化:字典类型——>序列化——>其他格式——>存 ...
- 简洁的实现ls-l命令
现在我们来简洁的实现命令:了解一下相关知识: 我们使用掩码可以很明确的得出文件的每一种信息.关于掩码,上图中的S_IRUSR等均为掩码.我们来看看函数getpwuid,原型:struct passwd ...
- unity 随笔
转载 慕容小匹夫 从游戏脚本语言说起,剖析Mono所搭建的脚本基础 深入浅出聊优化:从Draw Calls到GC 谁偷了我的热更新?Mono,JIT,IOS JS or C ...
- spring boot 访问jsp 弹出下载
在pom.xml中加入以下jar 包 <!-- 引入Spring Boot 内嵌的Tomcat对jsp的解析包--><dependency> <groupId>or ...
- 吴裕雄 24-MySQL 索引
MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度.打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是一 ...
- 吴裕雄 python神经网络 水果图片识别(5)
#-*- coding:utf-8 -*-### required libaraiedimport osimport matplotlib.image as imgimport matplotlib. ...
- scrapy 常用代码
一,scrapy请求 yield scrapy.Request(url=url, dont_filter=True, callback=self.page, meta={'item': item}) ...
- 根据获取的窗口句柄遍历窗口Edit控件
网上说遍历窗口控件有两种方法: 1),使用EnumChildWindows,没有深究, 学习网址如下:http://blog.sina.com.cn/s/blog_60ac1c4b010116 ...
- MATLAB二维相机标定的解决方案 calibration
第一步,在命令行下面输入cameraCalibrator,启动MATLAB相机标定.相机矫正界面 cameraCalibrator 第二步:拍照.如果你是做相机标定,你应该知道,你需要一些calibr ...