跟面试官确认是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 并集和交集的更多相关文章

  1. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  2. [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 ...

  3. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  4. [LintCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...

  5. No.023:Merge k Sorted Lists

    问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  6. Merge k Sorted Lists

    1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...

  7. 71. Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  8. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  9. Merge Two Sorted Lists

    Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...

随机推荐

  1. 飞利浦 PHILIPS 电动牙刷HX6730 拆解

    今日,一直比较喜欢用的电动牙刷,飞利浦HX6730坏掉了,初步感觉考虑飞利浦的保修,但是发现发票找不到了.飞利浦的客服也说,电动牙刷的两年保修依据分别是:1.发票开据日期:2.在无发票的情况下,看底部 ...

  2. harbor仓库镜像的删除

    harbor仓库镜像的删除 刚开始自己摸索了下,直接webui界面删除后,发现仓库空间未释放 上传之前仓库空间占用为 上传之后仓库空间占用为 在webui界面上删除镜像后 查看大小 依旧为286m,到 ...

  3. Delphi XE3通过ADOConnection 连接 MySQL 5.5.27 数据库

    Delphi XE3通过ADOConnection 连接 MySQL 5.5.27 数据库 unit Unit1; interface uses Winapi.Windows, Winapi.Mess ...

  4. powerdesiner技巧

    1.name和code同步问题和name成comments http://blog.csdn.net/huang_xw/article/details/5722981 2.连接数据库

  5. Android sdk测试方法链接

    https://blog.csdn.net/u013059441/article/details/79030998?utm_source=blogxgwz0

  6. Python之路 - Socket实现远程执行命令

    Python之路 - Socket实现远程执行命令 os模块实现

  7. java批量向oracle插入数据

    由于项目需要,需要将一个6M的txt中的数据插入到oracle数据表中.txt中的数据是每行一个词.经过统计,词总数是505040.为了看起来方便,我将我的所有方法写在类入口中,数据库的信息我会用te ...

  8. CSS 原理

    CSS是一个描述HTML文档的样式语言. CSS描述HTML元素的显示方式. 本教程将教你CSS从基础到网页布局,学完本教程,可以设计出漂亮的网站. 本教程需要HTML知识为基础,学习HTML前往&l ...

  9. 从源码安装Node

    [从源码安装Node] Nodejs官网未并提供i686架构的bin,为了在i686架构cpu下使用Nodejs,需要从源码编译. 1../configure 2.make 3.make instal ...

  10. NPM安装依赖速度慢问题

    [NPM安装依赖速度慢问题] npm config set registry http://registry.npm.taobao.org 参考:http://blog.csdn.net/rongbo ...