标签:
There are 2 sorted sets.Find the common elements of those sets
e.g.
A={1,2,3,4,5,6}
B={5,6,7,8,9}
o/p C={5,6}
Complexity should ne 0(n+m) where n and m is the size of the first and second set respectively
Which data structure should be used to store the output
List<int> FindIntersection(int[] A, int[] B) { int L = A.Length; int K = B.Length; List<int> intersectionArr = new ArrayList<int>(); int i = L - 1; int j = K - 1; while ((i >= 0) && (j >= 0)) { if (A[i] > B[j]) { i--; } else if (B[j] > A[i]) { j--; } else { intersectionArr.Add(A[i]); i--; j--; } } return intersectionArr; }
Common Element in Two Sorted Sets
标签:
原文地址:http://www.cnblogs.com/hygeia/p/5154494.html