// 给两个List a,b,实现两个List两个List的并集,即合并两个List,重复的元素只保留一个。
// 不可以调用现成的集合方法
// 有空闲限制,a的长度是m,b的长度是n,额外空间最多用m+n (不是O(m+n))
// 尝试优化下,最少的时间复杂度是多少,在代码注释里给出最终的时间复杂度
public static class ListUnion {
static List<Integer> union(List<Integer> a, List<Integer> b) {
// 在这儿实现
}
public static void main(String[] args) {
// union([ 2, 1, 3], [ 2, 3, 4]) == [1, 2, 3, 4]
System.out.println(union(Arrays.asList(2,1,3), Arrays.asList(2,3,4));
}
}
利用Map key值不能重复去做,遍历list直接塞进map
可以采用归并排序的方法来做,时间复杂度较低,不会超过平方的数量级