1. 程式人生 > >Union and Intersection of two sorted lists 並集和交集

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

跟面試官確認是arrayList還是singly-linked list

/*
Union 並集
@para a a list of non-null integer, in ascending order
@para b a list of non-null integer, in ascending order
@return the integers that are contained in either a or b, in ascending order
*/

 1 public class UnionTwoSortedLists {
 2     public  List<Integer> union(List<Integer> a, List<Integer> b) {
3 List<Integer> 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<Integer> l1 = new ArrayList<>(); 39 l1.add(1); 40 l1.add(2); 41 l1.add(3); 42 List<Integer> 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 交集
@para a a list of non-null integer, in ascending order
@para b a list of non-null integer, in ascending order
@return the integers that are contained in both a or b, in ascending order

*/

 1 public class IntersectionTwoSortedLists {
 2 
 3     public List<Integer> intersection(List<Integer> a, List<Integer> b) {
 4         List<Integer> 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<Integer> l1 = new ArrayList<>();
29         l1.add(1);
30         l1.add(2);
31         l1.add(3);
32         List<Integer> 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