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;
}
时间: 2024-10-11 10:16:54

Common Element in Two Sorted Sets的相关文章

leetcode 378. Kth Smallest Element in a Sorted Matrix

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix = [ [ 1, 5

【Leetcode】Kth Smallest Element in a Sorted Matrix

题目链接:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ 题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest eleme

Kth Smallest Element in a Sorted Matrix -- LeetCode

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix = [ [ 1, 5

LeetCode-Kth Smallest Element in a Sorted Matrix

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix = [ [ 1, 5

[LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix = [ [ 1, 5

Redis(2.8.3) 命令学习 - Sorted Sets

ZADD key score member [score member ...] Add one or more members to a sorted set, or update its score if it already exists 127.0.0.1:6379> ZADD foo 1 one (integer) 1 127.0.0.1:6379> ZADD foo 2 two (integer) 1 127.0.0.1:6379> ZADD foo 3 three (int

【Leetcode】378. Kth Smallest Element in a Sorted Matrix

Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix

redis之sorted sets类型及操作

sorted sets类型及操作 sorted set是set的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的值调整顺序.可以理解为有两列的mysql表,一列存value,一列存顺序.操作中key理解为zset的名字. 和set一样sorted set也是string类型元素的集合,不同的是每个元素都会关联一个double类型的score.sorted set的实现是skip list和hash table的混合体.

Redis数据类型:Sorted Sets操作指令

Redis数据类型:Sorted Sets操作指令 Sorted Sets常用操作指令 Sorted Sets,本质是一个有序的Sets,其实在原来的Sets集合中对每一个元素新增了一个属性Score,用于排序. ZADD 将指定的元素及Score添加到集合.如果集合中存在该元素,则更新其Score. 如果集合不存在,会先创建一个集合然后在添加元素及Score. 127.0.0.1:6379> ZADD sortset 1 name (integer) 1 ZRANGE 返回指定下标开始到结束下