输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。
1. 0<=pushV.length == popV.length <=1000
2. -1000<=pushV[i]<=1000
3. pushV 的所有数字均不相同
[1,2,3,4,5],[4,5,3,2,1]
true
可以通过push(1)=>push(2)=>push(3)=>push(4)=>pop()=>push(5)=>pop()=>pop()=>pop()=>pop() 这样的顺序得到[4,5,3,2,1]这个序列,返回true
[1,2,3,4,5],[4,3,5,1,2]
false
由于是[1,2,3,4,5]的压入顺序,[4,3,5,1,2]的弹出顺序,要求4,3,5必须在1,2前压入,且1,2不能弹出,但是这样压入的顺序,1又不能在2之前弹出,所以无法形成的,返回false
# -*- coding:utf-8 -*- class Solution: def IsPopOrder(self, pushV, popV): # write code here stack = [] index = 0 for inV in pushV: stack.append(inV) while stack and stack[-1] == popV[index]: index +=1 stack.pop() return len(stack)==0
# -*- coding:utf-8 -*- # 基本思想:按入栈顺序,将pushV的元素依次入stack栈,如果栈顶元素等于popV的第一个元素, # 此时要将stack的栈顶元素出栈,并丢掉popV的第一个元素,此时,如果栈顶元素仍然等于popV的 # 第一个元素,则继续出战,直到值不相等时,继续将pushV的元素入栈,反复操作直到pushV为空。 # 此时只需要判断stack和popV是否相反,如果是,返回true, 否则返回false。 def IsPopOrder(self, pushV, popV): # write code here if not pushV&nbs***bsp;not popV: return False stack = [] # 定义一个栈 equal = False # 标志位,如果为true,则继续出栈操作,不进行入栈操作 while pushV: if not equal or not stack: stack.append(pushV.pop(0)) if stack[-1] != popV[0]: equal = False continue equal = True stack.pop() popV.pop(0) if len(stack) != len(popV): return False while stack: if stack.pop() != popV.pop(0): return False return True
# -*- coding:utf-8 -*- class Solution: def IsPopOrder(self, pushV, popV): # write code here if pushV==None&nbs***bsp;popV==None: if pushV==None and popV==None: return True else: return False n,m=len(pushV),len(popV) if n != m: return False else: if set(pushV) != set(popV): return False first_push=pushV.index(popV[0]) tmp=pushV[:first_push] pushV=pushV[first_push+1:] for i in range(1,n): if tmp: if popV[i]==tmp[-1]: tmp.pop() elif popV[i] in pushV: tmp+=pushV[:pushV.index(popV[i])] pushV=pushV[pushV.index(popV[i])+1:] else: return False elif popV[i] in pushV: tmp+=pushV[:pushV.index(popV[i])] pushV=pushV[pushV.index(popV[i])+1:] else: return False return True
# -*- coding:utf-8 -*- class Solution: def IsPopOrder(self, pushV, popV): result = [] index = 0 for i in pushV: result.append(i) while result != [] and result[-1] == popV[index]: result.pop() index += 1 if result == []: return True return False
python解法:
解法1:新建一个栈,将数组A压入栈s中,当栈顶元素等于数组B元素时且栈s不为空时,将s出栈。
循环结束时,判断栈s是否为空,若为空则返回true;
class Solution: def IsPopOrder(self, pushV, popV): s = [] i,j = 0,0 for i in pushV: s.append(i) # 当栈顶元素等于数组B元素时且栈s不为空时,将s出栈 while s and s[-1] == popV[j]: s.pop() j += 1 return len(s) == 0
# -*- coding:utf-8 -*- class Solution: def IsPopOrder(self, pushV, popV): # write code here stack=[] i,j=0,0 while i<len(pushV): if pushV[i]!=popV[j]: stack.append(pushV[i]) i+=1 elif pushV[i]==popV[j]: i+=1 j+=1 while stack and (popV[j]==stack[-1]): stack.pop() j+=1 if not stack: return True else: return False
class Solution: def IsPopOrder(self, pushV, popV): stack = [] index = 0 for item in pushV: stack.append(item) if item == popV[index]: stack.pop() index += 1 if stack[::-1] == popV[index:]: return True return False
逐个匹配出栈字符串的字符:
如果当前栈顶是这个字符,则弹出,匹配下一个出栈字符;
如果当前栈顶不是这个字符,则说明需要继续压栈直到栈顶是这个字符;
如果压栈字符串所有字符压入都没有栈顶是这个字符的情况,说明该出栈序列不正确。
# -*- coding:utf-8 -*- class Solution: def IsPopOrder(self, pushV, popV): # write code here s = [] i_push = 0 i_pop = 0 while i_push < len(pushV) or i_pop < len(popV): while len(s)==0 or s[-1]!=popV[i_pop]: if i_push == len(pushV): return False s.append(pushV[i_push]) i_push+=1 else: s.pop() i_pop += 1 if i_pop == len(popV): return True return False
# 核心思路: 对于pop出来的每一个数,在他之后出栈的那些元素,如果入栈比他早,那么这些元素 # 的出栈顺序一定是按其入栈顺序的倒序 出栈的 class Solution: def IsPopOrder(self, pushV, popV): # write code here if sorted(pushV) != sorted(popV): return False cur = 0 for i in range(len(popV)): if i <= cur:continue # 判断过的不用再判断了 order_list = [] i_in = pushV.index(popV[i]) for j in range(i,len(popV)):# 遍历它之后的元素 if pushV.index(popV[j]) < i_in: # 如果入栈顺序比它早 order_list.append(pushV.index(popV[j])) # 加入考察的范围 if order_list == sorted(order_list,reverse=True): # 这些入栈更早的元素 必须严格遵循先入后出 cur = i else: return False return True
# -*- coding:utf-8 -*- class Solution: def IsPopOrder(self, pushV, popV): # write code here if pushV == []&nbs***bsp;len(pushV) != len(popV): return None stack = [] index = 0 for i in pushV: stack.append(i) while stack and stack[-1] == popV[index]: stack.pop() index += 1 return True if stack == [] else False注意:stack[-1]为什么换成stack[0]不行呢?因为当对于stack不断append时,此时stack由于不等于popV[index]所以不会立刻弹出,也就是说stack此时不止存放一个数。然后我们比的是stack最后的一个数,所以用的是stack[-1]。