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 ...
随机推荐
- nodejs 解决跨域
1.失败 app.all('*', function (req, res, next) { res.header("Access-Control-Allow-Origin", &q ...
- 利用docker-machine安装swarm
转自:https://www.cnblogs.com/jsonhc/p/7832642.html 安装之前的环境: 两个节点,节点1:192.168.101.14,用来创建manager1 machi ...
- Mybatis动态sql及性能优化-3
内容简介 1.回顾 2.动态sql 3.性能优化 懒加载机制 一级缓存 二级缓存 一.回顾 1.config文件常用标签 properties标签:引入外部properties文件资源. settin ...
- centos6.9出现openvpn:error=certificate signature failure的处理
原因: 将原来openwrt上用的证书复制到centos 6.9后,客户端都连不上了,查了服务器log,出现是error=certificate signature failure错误. 处理方法见帖 ...
- Structs复习 Result第一部分
Jar包 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&q ...
- 如何查看Python对象的属性
在Python语言中,有些库在使用时,在网络上找到的文档不全,这就需要查看相应的Python对象是否包含需要的函数或常量.下面介绍一下,如何查看Python对象中包含哪些属性,如成员函数.变量等,其中 ...
- EF AutoMaper
Mapper.CreateMap<Source,Dest>(); 该方法已弃用,使用下面这个 Mapper.Initialize(x=>x.CreateMap<Source,D ...
- anchor_generator.proto:11:3: Expected "required", "optio nal", or "repeated"
转自:https://github.com/tensorflow/models/issues/1834 When I use the commond " protoc object_dete ...
- Latex公式示范
\(A_\alpha(x)\) \(\qquad\) \(a^2+b^2=c^2 \) \(\qquad\) \(\sum\limits_{m=0}^\inft ...
- 用pandas读取excel报错
用pandas.read_execl()方法读取excel文件报错. 后来导入xlrd第三方库,就好了.