题解 | #栈的压入、弹出序列#
栈的压入、弹出序列
https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pushV int整型一维数组
* @param pushVLen int pushV数组长度
* @param popV int整型一维数组
* @param popVLen int popV数组长度
* @return bool布尔型
*/
//构建栈
struct StackL {
int data[2000];
int top;
} Stack;
//压入元素
void push(int number) {
Stack.data[Stack.top++] = number;
}
//弹出元素
void pop() {
--Stack.top;
}
//栈顶元素
int Top() {
// return Stack.data[Stack.top--];
//不需要弹出栈顶元素
return Stack.data[Stack.top -1];
}
//pushV压入,pushVlen压入元素的长度
bool IsPopOrder(int* pushV, int pushVLen, int* popV, int popVLen ) {
// write code here
Stack.top = 0;
int i = 0, j = 0;
while (i < pushVLen) {
push(pushV[i]);
i++;
while (Stack.top > 0 && popV[j] == Top()) {
pop();
j++;
}
}
return Stack.top == 0 && popVLen == j;
}

