查找 Stack 和 Queue 的共同元素
给定一堆M元素和一个按排序顺序排列的N个元素的队列。任务是找出栈和队列的共同元素。
例子:
Input: stack = [1, 3, 5, 7], queue = [1, 2, 5, 9]
Output: 5, 1
Explanation: 1 and 5 is present in both stack and queue.
Input: stack = [1, 3], queue = [2, 4]
Output: Not Found
Explanation: There is no common element.
方法:给定的问题可以借助以下思想来解决:
As both are sorted, the top element of the stack will be the maximum and the front of the queue will be the minimum. So reverse any of them and compare the elements in top of stack and front of queue to find the common elements.
请按照下图更好地理解。
插图:
Say, stack = [1, 3, 5, 7] where 7 is at the top and
the queue = [1, 2, 5, 9] where 1 is at the front.
Say we are reversng the queue. Do the following to reverse the queue:
- In first step:
One by one pop the element from the queue(i.e., all the elements of queue) and push into stack.
=> After First Iteration, Stack = [1, 3, 5, 7, 1] and Queue = [2, 5, 9]
=> After Second Iteration, Stack = [1, 3, 5, 7, 1, 2] and Queue = [5, 9]
=> After Third Iteration, Stack = [1, 3, 5, 7, 1, 2, 5] and Queue = [9]
=> After Fourth Iteration, Stack = [1, 3, 5, 7, 1, 2, 5, 9] and Queue = [] - In second step:
=> One by one pop the element from the stack(i.e., coming from queue) and push into queue.
=> After First Iteration, Stack = [1, 3, 5, 7, 1, 2, 5] and Queue = [9]
=> After Second Iteration, Stack = [1, 3, 5, 7, 1, 2] and Queue = [9, 5]
=> After Third Iteration, Stack = [1, 3, 5, 7, 1] and Queue = [9, 5, 2]
=> After Fourth Iteration, Stack = [1, 3, 5, 7] and Queue = [9, 5, 2, 1]
Now the following for finding the common elements.
1st Step:
=> stack top < queue front.
=> Pop queue front.
=> So stack is [1, 3, 5, 7] and queue [5, 2, 1]
2nd step:
=> stack top > queue front
=> Pop stack top
=> So stack [1, 3, 5] and queue [5, 2, 1]
3rd step:
=> stack top = queue front
=> Pop stack top and queue front
=> So stack [1, 3] and queue [2, 1]
=> Common elements [5]
4th step:
=> stack top > queue front
=> Pop stack top
=> So stack [1] and queue [2, 1]
5th Step:
=> stack top < queue front.
=> Pop queue front.
=> So stack is [1] and queue [1]
6th step:
=> stack top = queue front
=> Pop stack top and queue front
=> So stack [] and queue []
=> Common elements [5, 1].
请按照以下步骤解决问题:
- 反转队列。
- 遍历栈和队列,同时栈和队列不为空。
- 如果栈顶=队列的前面,那是一个公共元素。
- 否则,如果栈顶 > 队列前面,则弹出栈顶元素。
- 否则,栈顶<队列前端,弹出栈顶元素。
- 打印公共元素。
下面是上述方法的实现:
C++
// C++ code to implement the above approach
#include
using namespace std;
// Function to find common element
// of stack and queue
vector findCommonElement(stack& St,
queue& Q)
{
// Initialize size of queue Q to 0
int Size = 0;
vector v;
// Put every element of queue into stack
// and calculate size of queue
while (!Q.empty()) {
St.push(Q.front());
Q.pop();
Size++;
}
// Put extra element of stack into queue
// again extra element of stack is the
// element coming from queue. Now, the
// queue is reverse
while (Size != 0) {
Q.push(St.top());
St.pop();
Size--;
}
// Traverse while stack and queue is not
// empty
while (!St.empty() && !Q.empty()) {
// Top element of stack
int a = St.top();
// Front element of queue
int b = Q.front();
// Push the common element
// in vector if a = b
if (a == b)
v.push_back(a);
// Else pop the larger value
// from its container
(a > b) ? St.pop() : Q.pop();
}
return v;
}
// Driver Code
int main()
{
stack St;
queue Q;
// Fill element into stack
St.push(1);
St.push(3);
St.push(5);
St.push(7);
// Fill element into queue
Q.push(1);
Q.push(2);
Q.push(5);
Q.push(9);
// Find common element if exists
vector v = findCommonElement(St, Q);
if (v.size() == 0)
cout << "Not Found" << endl;
for (auto i : v)
cout << i << " ";
return 0;
}
5 1
时间复杂度: O(M+N)
辅助空间: O(1)