题解 | #栈的压入、弹出序列#
栈的压入、弹出序列
https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106
class Solution:
def IsPopOrder(self, pushV: List[int], popV: List[int]) -> bool:
# 辅助数组
s = []
# 入栈的index
j = 0
n = len(pushV)
for i in range(n):
# 入栈
while j<n and (s == [] or s[-1]!=popV[i]):
s.append(pushV[j])
j += 1
if s[-1]==popV[i]:
s.pop()
else:
return False
return True
记得在判断的时候写判断s是否为空 因为如果s为空的时候s[-1]是没有意义的,会产生数组的越界
查看14道真题和解析