在不改变顺序的情况下交换 Stack 和 Queue 的元素
给定一个包含M个元素的堆栈St和一个包含N个元素的队列Q。任务是将堆栈的每个元素放入队列,并将队列的每个元素放入堆栈,而不改变它们的顺序。
例子:
Input: St = {4, 3, 2, 1}, Q = {8, 7, 6, 5}
Output: St = {8, 7, 6, 5}, Q = {1, 2, 3, 4}
Input: St = {0, 1}, Q = {2, 3}
Output: St = {2, 3}, Q = {0, 1}
Naive Approach:基本的做法是将堆栈和队列的元素存储在一个单独的数组中,然后再次将它们放入队列和堆栈中。
时间复杂度: O(N + M)
辅助空间: O(N + M)
优化方法:利用栈和队列的后进先出和先进先出特性,可以在不占用任何额外空间的情况下解决这个问题。
请按照以下步骤解决问题:
- 将队列中的每个元素放入堆栈。
- 将栈中多余的元素再次放入队列,栈中多余的元素就是队列中来的元素。现在,队列颠倒了。
- 现在,将堆栈的每个元素放入队列中。
- 最后,将队列的原始元素放入堆栈。
- 现在再次重复第一步和第二步,以保持队列中堆栈元素的顺序。
请按照以下插图更好地理解该方法:
插图:
Consider the stack = [1, 2, 3, 4] where 1 is at the top and the queue = [8, 7, 6, 5] where 8 is at the front.
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 = [8, 1, 2, 3, 4] and queue = [7, 6, 5]
After Second Iteration, Stack = [7, 8, 1, 2, 3, 4] and queue = [6, 5]
After Third Iteration, Stack = [6, 7, 8, 1, 2, 3, 4] and queue = [5]
After Fourth Iteration, Stack = [5, 6, 7, 8, 1, 2, 3, 4] 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 = [6, 7, 8, 1, 2, 3, 4] and queue = [5]
After Second Iteration, Stack = [7, 8, 1, 2, 3, 4] and queue = [5, 6]
After Third Iteration, Stack = [8, 1, 2, 3, 4] and queue = [5, 6, 7]
After Fourth Iteration, Stack = [1, 2, 3, 4] and queue = [5, 6, 7, 8]
In third step:
One by one pop the element from the stack(i.e., remaining all the elements) and push into queue.
After First Iteration, Stack = [2, 3, 4] and queue = [5, 6, 7, 8, 1]
After Second Iteration, Stack = [3, 4] and queue = [5, 6, 7, 8, 1, 2]
After Third Iteration, Stack = [4] and queue = [5, 6, 7, 8, 1, 2, 3]
After Fourth Iteration, Stack = [] and queue = [5, 6, 7, 8, 1, 2, 3, 4]
In fourth step:
One by one pop the element from the queue(i.e., only element of queue before first step) and push into stack.
After First Iteration, Stack = [5] and queue = [6, 7, 8, 1, 2, 3, 4]
After Second Iteration, Stack = [6, 5] and queue = [7, 8, 1, 2, 3, 4]
After Third Iteration, Stack = [7, 6, 5] and queue = [8, 1, 2, 3, 4]
After Fourth Iteration, Stack = [8, 7, 6, 5] and queue = [1, 2, 3, 4]
Now repeat first and second step.
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to Put every element of stack
// into queue and queue into stack
// without changing its order
void changeElement(stack& St,
queue& Q)
{
// Calculate size of queue Q
int Size = Q.size();
int Temp = Size;
int N = St.size();
// Put every element of queue into stack
while (!Q.empty()) {
St.push(Q.front());
Q.pop();
}
// Put extra element of stack into
// queue again, extra element of stack
// is the element coming from queue.
// Now, the queue is reversed
while (Size != 0) {
Q.push(St.top());
St.pop();
Size--;
}
// Put every element of stack into queue
while (!St.empty()) {
Q.push(St.top());
St.pop();
}
Size = Temp;
// Put initial element of queue
// into stack
while (Size != 0) {
St.push(Q.front());
Q.pop();
Size--;
}
// Repeat the first and second steps
while (!Q.empty()) {
St.push(Q.front());
Q.pop();
}
while (N != 0) {
Q.push(St.top());
St.pop();
N--;
}
}
// Function to traverse till stack is
// not empty and print the element in it
void printStack(stack& St)
{
while (!St.empty()) {
cout << St.top() << " ";
St.pop();
}
cout << endl;
}
// Function to traverse till queue is not
// empty and print the element in it
void printQueue(queue& Q)
{
while (!Q.empty()) {
cout << Q.front() << " ";
Q.pop();
}
cout << endl;
}
// Driver Code
int main()
{
stack St;
queue Q;
// Fill element into stack
St.push(4);
St.push(3);
St.push(2);
St.push(1);
// Fill element into queue
Q.push(8);
Q.push(7);
Q.push(6);
Q.push(5);
changeElement(St, Q);
cout << "Stack = ";
printStack(St);
cout << "Queue = ";
printQueue(Q);
return 0;
}
Python3
# python3 program for the above approach
import collections
# Function to Put every element of stack
# into queue and queue into stack
# without changing its order
def changeElement(St, Q):
# Calculate size of queue Q
Size = len(Q)
Temp = Size
N = len(St)
# Put every element of queue into stack
while (len(Q) != 0):
St.append(Q.popleft())
# Put extra element of stack into
# queue again, extra element of stack
# is the element coming from queue.
# Now, the queue is reversed
while (Size != 0):
Q.append(St.pop())
Size -= 1
# Put every element of stack into queue
while (len(St) != 0):
Q.append(St.pop())
Size = Temp
# Put initial element of queue
# into stack
while (Size != 0):
St.append(Q.popleft())
Size -= 1
# Repeat the first and second steps
while (len(Q) != 0):
St.append(Q.popleft())
while (N != 0):
Q.append(St.pop())
N -= 1
# Function to traverse till stack is
# not empty and print the element in it
def printStack(St):
while (len(St) != 0):
print(St.pop(), end=" ")
print()
# Function to traverse till queue is not
# empty and print the element in it
def printQueue(Q):
while (len(Q) != 0):
print(Q.popleft(), end=" ")
print()
# Driver Code
if __name__ == "__main__":
St = collections.deque()
Q = collections.deque()
# Fill element into stack
St.append(4)
St.append(3)
St.append(2)
St.append(1)
# Fill element into queue
Q.append(8)
Q.append(7)
Q.append(6)
Q.append(5)
changeElement(St, Q)
print("Stack = ", end="")
printStack(St)
print("Queue = ", end="")
printQueue(Q)
# This code is contributed by rakeshsahni
Stack = 8 7 6 5
Queue = 1 2 3 4
时间复杂度:O(M+N)
辅助空间:O(1)