Union and Intersection of two sorted lists 并集和交集

Union and Intersection of two sorted lists 并集和交集_第1张图片

 

跟面试官确认是arrayList还是singly-linked list

/*
  Union 并集:两个升序的list a, b, 返回其并集(升序排序)
*/

 1 public class UnionTwoSortedLists {
 2     public  List union(List a, List b) {
 3         List result = new ArrayList<>();
 4         // Use two index variables i and j, initial values i = 0, j = 0
 5         int i = 0, j = 0;
 6         while (i < a.size() && j < b.size()) {
 7             // If a.get(i) < b.get(j) then add a.get(i) to result and increment i.
 8             if (a.get(i) < b.get(j)) {
 9                 result.add(a.get(i));
10                 i++;
11             }
12             // If b.get(j) < a.get(i) then add b.get(j) to result and increment j.
13             else if (b.get(j) < a.get(i)) {
14                 result.add(b.get(j));
15                 j++;
16 
17             }
18             // If both are same then add any to result and increment both i and j.
19             else {
20                 result.add(b.get(j));
21                 i++;
22                 j++;
23             }
24         }
25        /* handle the case a.size()!= b.size(), add remaining elements of the larger array */
26         while (i < a.size()){
27             result.add(a.get(i));
28             i++;
29         }
30         while (j  < b.size()){
31             result.add(b.get(j));
32             j++;
33         }
34         return result;
35     }
36 
37     public static void main(String[] args) {
38         List l1 = new ArrayList<>();
39         l1.add(1);
40         l1.add(2);
41         l1.add(3);
42         List l2 = new ArrayList<>();
43         l2.add(2);
44         l2.add(3);
45         UnionTwoSortedLists test = new UnionTwoSortedLists();
46         for(int n : test.union(l1,l2)){
47             System.out.println(n);
48         }
49     }
50 }

 

 

/*
   Intersection 交集:两个升序的list a, b, 返回其交集(升序排序)

*/

 1 public class IntersectionTwoSortedLists {
 2 
 3     public List intersection(List a, List b) {
 4         List result = new ArrayList<>();
 5         // Use two index variables i and j, initial values i = 0, j = 0
 6         int i = 0, j = 0;
 7         while (i < a.size() && j < b.size()) {
 8             // If a.get(i) < b.get(j) then increment i.
 9             if (a.get(i) < b.get(j)) {
10                 i++;
11             }
12             // If b.get(j) < a.get(i) then increment j.
13             else if (b.get(j) < a.get(i)) {
14                 j++;
15 
16             }
17             // If both are same then add any to result and increment both i and j.
18             else {
19                 result.add(b.get(j));
20                 i++;
21                 j++;
22             }
23         }
24         return result;
25     }
26 
27     public static void main(String args[]) {
28         List l1 = new ArrayList<>();
29         l1.add(1);
30         l1.add(2);
31         l1.add(3);
32         List l2 = new ArrayList<>();
33         l2.add(2);
34         l2.add(5);
35         IntersectionTwoSortedLists test = new IntersectionTwoSortedLists();
36         for (int n : test.intersection(l1, l2)) {
37             System.out.println(n);
38         }
39     }
40 }

 

 

followup1: 上述问题是2个list,  求问n个list的union和intersection

 

转载于:https://www.cnblogs.com/liuliu5151/p/9899558.html

你可能感兴趣的:(Union and Intersection of two sorted lists 并集和交集)