跟面试官确认是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. spring获取配制文件的参数

    项目中需要获取一些万年不变的参数,比如单点登录的域名 怎么从多个文件配置中获取呢,原来spring早已经提供了类PropertyPlaceholderConfigurer <?xml versi ...

  2. Delphi 泛型详解

    http://www.cnblogs.com/jxgxy/category/216671.html

  3. cvc-complex-type.2.4.a: Invalid content was found starting with element 'async-supported'. One of '{"http://java.sun.com/xml/ns/javaee":init-param}' is expected.

    第一种方案:  将  "http://java.sun.com/xml/ns/javaee"  换为  "http://java.sun.com/xml/ns/j2ee& ...

  4. 用ADO操作数据库的方法步骤(ZT)

    http://www.cppblog.com/changshoumeng/articles/113437.html 学习ADO时总结的一些经验 用ADO操作数据库的方法步骤 ADO接口简介 ADO库包 ...

  5. Kotlin语言学习笔记(2)

    类(classes) // 类声明 class Invoice { } // 空的类 class Empty // 主体构造器(primary constructor) class Person co ...

  6. IE (第二部分) 浏览器 中 关于浏览器模式和文本模式

    判断真正的 IE 版本 很多 JS 框架都通过 UA 判断 IE 的版本.对于 IE6,这种做法没问题( IE6 没有浏览器模式的概念,也没有其它 IE 可以把浏览器模式改为 IE6:IE7 虽然也没 ...

  7. 吴裕雄 22-MySQL 事务

    MySQL 事务MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据 ...

  8. java的特点

    java是一种跨平台.适合于分布式计算机环境的面向对象编程语言.具有以下特性:简单性.面向对象.分布性.解释性.可靠.安全.平台无关.可移植性.高性能.多线程.动态性等特点. 面向过程和面向对象可以用 ...

  9. centos 安装mysql数据库

    在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 1 下载并安装MySQL官方的 Yum Re ...

  10. export default 与 export

    export default 只能导出一个 可以用任意的变量来接收 export 可以暴露多个成员,需要用 import {} 接受成员 需要用名字接受 名字必须跟导出名字一致  //或者as作为别名 ...