检查给定的 Stack 的 push 和 pop 序列是否有效
给定具有不同值的 push[] 和 pop[] 序列。任务是检查这是否可能是对最初为空的堆栈进行的一系列推送和弹出操作的结果。如果返回“False”,则返回“True”。
例子:
Input: pushed = { 1, 2, 3, 4, 5 }, popped = { 4, 5, 3, 2, 1 }
Output: True
Following sequence can be performed:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Input: pushed = { 1, 2, 3, 4, 5 }, popped = { 4, 3, 5, 1, 2 }
Output: False
1 can't be popped before 2.
方法:如果元素 X 已被压入堆栈,则检查 pop[] 序列中的顶部元素是否为 X。如果是 X 则立即弹出它,否则 push[] 序列的顶部将被更改并使序列无效。因此,类似地,对所有元素执行相同的操作,并检查堆栈是否为空。如果为空,则打印True否则打印False 。
以下是上述方法的实现:
C++
// C++ implementation of above approach
#include
#include
using namespace std;
// Function to check validity of stack sequence
bool validateStackSequence(int pushed[], int popped[], int len){
// maintain count of popped elements
int j = 0;
// an empty stack
stack st;
for(int i = 0; i < len; i++){
st.push(pushed[i]);
// check if appended value is next to be popped out
while (!st.empty() && j < len && st.top() == popped[j]){
st.pop();
j++;
}
}
return j == len;
}
// Driver code
int main()
{
int pushed[] = {1, 2, 3, 4, 5};
int popped[] = {4, 5, 3, 2, 1};
int len = sizeof(pushed)/sizeof(pushed[0]);
cout << (validateStackSequence(pushed, popped, len) ? "True" : "False");
return 0;
}
// This code is contributed by Rituraj Jain
Java
// Java program for above implementation
import java.util.*;
class GfG
{
// Function to check validity of stack sequence
static boolean validateStackSequence(int pushed[],
int popped[], int len)
{
// maintain count of popped elements
int j = 0;
// an empty stack
Stack st = new Stack<>();
for (int i = 0; i < len; i++)
{
st.push(pushed[i]);
// check if appended value
// is next to be popped out
while (!st.empty() && j < len &&
st.peek() == popped[j])
{
st.pop();
j++;
}
}
return j == len;
}
// Driver code
public static void main(String[] args)
{
int pushed[] = {1, 2, 3, 4, 5};
int popped[] = {4, 5, 3, 2, 1};
int len = pushed.length;
System.out.println((validateStackSequence(pushed, popped, len) ? "True" : "False"));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Function to check validity of stack sequence
def validateStackSequence(pushed, popped):
# maintain count of popped elements
j = 0
# an empty stack
stack = []
for x in pushed:
stack.append(x)
# check if appended value is next to be popped out
while stack and j < len(popped) and stack[-1] == popped[j]:
stack.pop()
j = j + 1
return j == len(popped)
# Driver code
pushed = [1, 2, 3, 4, 5]
popped = [4, 5, 3, 2, 1]
print(validateStackSequence(pushed, popped))
C#
// C# program for above implementation
using System;
using System.Collections.Generic;
class GfG
{
// Function to check validity of stack sequence
static bool validateStackSequence(int []pushed,
int []popped, int len)
{
// maintain count of popped elements
int j = 0;
// an empty stack
Stack st = new Stack();
for (int i = 0; i < len; i++)
{
st.Push(pushed[i]);
// check if appended value
// is next to be popped out
while (st.Count != 0 && j < len &&
st.Peek() == popped[j])
{
st.Pop();
j++;
}
}
return j == len;
}
// Driver code
public static void Main(String[] args)
{
int []pushed = {1, 2, 3, 4, 5};
int []popped = {4, 5, 3, 2, 1};
int len = pushed.Length;
Console.WriteLine((validateStackSequence(pushed,
popped, len) ? "True" : "False"));
}
}
// This code contributed by Rajput-Ji
PHP
Javascript
输出:
True
时间复杂度: O(N),其中 N 是堆栈的大小。