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 ...
随机推荐
- JSP基本
JSPとは.HTMLファイルにJavaコードを埋め込んでおき.クライアントの要求に応じてコードを実行.処理結果のみをクライアントに送信する技術です. 1.JSPは実はサーブレットです.最初にリクエスト ...
- Graylog日志管理系统---搜索查询方法使用简介
Elasticsearch 是一个基于 Lucene 构建的开源.分布式.提供 RESTful 接口的全文搜索引擎 一.Search页面的各位置功能介绍: 1.日志搜索的时间范围 为了使用方便,预设有 ...
- NetStream 记录
bufferLength : Number [只读] 数据当前存在于缓冲区中的秒数.(已进入缓冲区的秒数) bufferTime : Number 指定在开始显示流之前需要多长时间将消息存入缓冲区.( ...
- unity "[ ]"标签
[CanEditMultipleObjects]//可多对象编辑 public class Collider2DEditor:Editor {} [SerializeField]//序列化私有属性 p ...
- JS 打印图片
在使用window.print()进行打印时,打印的内容可能会包含图片内容,此时的图片内容不能设置为背景图片,否则将无法再打印页面显示. <!doctype html> <html& ...
- Spring3.0学习1.2(使用annotation)
使用annotation 首先 xml文件更改 新加xslt <?xml version="1.0" encoding="UTF-8"?> < ...
- python第三步骤(pygame)
1:先安装homebrew(类似于yum /apt-get为什么需要它呢,因为pip安装的时候需要很多的包的依赖,sdl什么的), 2:pip 安装pygame 我讨厌的环境变量问题 然后 通过的是 ...
- HTML5的新标签之一的Canvas
一. <canvas>简介(了解) 1. 什么是canvas: 是HTML5提供的一种新标签 <canvas></canvas> 英 ['kænvəs] 美 [ ...
- web.xml中的load-on-startup
1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法). 2)它的值必须是一个整数,表示servlet应该被载入的顺序 2)当值为0或 ...
- linux服务器中Jenkins集成git、Gradle持续构建Springboot项目
Jenkins是用java编写的开源持续集成工具,目前被国内外各公司广泛使用.本章教大家如何在linux服务器中使用Jenkins自动发布一个可作为linux服务发布的Springboot项目. 自动 ...