Common Element in Two Sorted Sets

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Common Element in Two Sorted Sets相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

algorithm@ find kth smallest element in two sorted arrays (O(log n time)

Leetcode 34. Find First and Last Position of Element in Sorted Array

How to initialize a two-dimensional array in Python?

selenium之 坑(StaleElementReferenceException: Message: Element not found in the cache...)

Majority Element II; Array; Two Pointers;

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible处理方法:selenium针对下拉