首页 > 其他 > 详细

Common Element in Two Sorted Sets

时间:2016-01-24 06:57:13      阅读:144      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!