No29栈的push、pop序列(栈)
2014-05-25 15:20
260 查看
题目博客:
http://blog.csdn.net/v_JULY_v/article/details/6057286
题目:
题目:输入两个整数序列。其中一个序列表示栈的push顺序,
判断另一个序列有没有可能是对应的pop顺序。
为了简单起见,我们假设push序列的任意两个整数都是不相等的。
比如输入的push序列是1、2、3、4、5,那么4、5、3、2、1就有可能是一个pop系列。
因为可以有如下的push和pop序列:
push 1,push 2,push 3,push 4,pop,push 5,pop,pop,pop,pop,
这样得到的pop序列就是4、5、3、2、1。
但序列4、3、5、1、2就不可能是push序列1、2、3、4、5的pop序列。
代码为:
import copy
#from wx import COPY
import wx
from wx import COPY
def CanPopOut(push_in):
return GetPopOut_All([[]], push_in, [])
def GetPopOut_All(old_possible_list,push_value,curren_stack):
from __builtin__ import len
if len(push_value)==0:
templist=[]
while curren_stack:
templist.append(curren_stack.pop(-1))
new_possible_list=copy.deepcopy(old_possible_list)
for inner_list in new_possible_list:
inner_list.extend(templist)
return new_possible_list
else:
final_re=[]
assert type(old_possible_list[0])==list,"Error with list"
if len(curren_stack)!=0:
tostack_newlist=copy.deepcopy(old_possible_list)
tostack_newstack=curren_stack[:]
tostack_newpush=push_value[:]
popedvalue=tostack_newstack.pop(-1)
for innerlist in tostack_newlist:
innerlist.append(popedvalue)
tostack_re=GetPopOut_All(tostack_newlist, tostack_newpush, tostack_newstack)
final_re.extend(tostack_re)
push_to_stack_list=copy.deepcopy(old_possible_list)
pushtostack_newstack=curren_stack[:]
pushtostack_newpush=push_value[:]
pushtostack_newstack.append(pushtostack_newpush.pop(0))
pushtostack_re=GetPopOut_All(push_to_stack_list, pushtostack_newpush, pushtostack_newstack)
final_re.extend(pushtostack_re)
return final_re
def GetStrFromList(inlist):
#from _ast import Str
return ''.join([str(item)for item in inlist])
if __name__ == '__main__':
pushin_list=[1,2,3,4,5]
from __builtin__ import str
strlist=[GetStrFromList(inner_list) for inner_list in CanPopOut(pushin_list)]
ceshi_list=[4,3,5,1,2]
for tempstr in strlist:
print tempstr
print GetStrFromList(ceshi_list) in strlist
http://blog.csdn.net/v_JULY_v/article/details/6057286
题目:
题目:输入两个整数序列。其中一个序列表示栈的push顺序,
判断另一个序列有没有可能是对应的pop顺序。
为了简单起见,我们假设push序列的任意两个整数都是不相等的。
比如输入的push序列是1、2、3、4、5,那么4、5、3、2、1就有可能是一个pop系列。
因为可以有如下的push和pop序列:
push 1,push 2,push 3,push 4,pop,push 5,pop,pop,pop,pop,
这样得到的pop序列就是4、5、3、2、1。
但序列4、3、5、1、2就不可能是push序列1、2、3、4、5的pop序列。
代码为:
import copy
#from wx import COPY
import wx
from wx import COPY
def CanPopOut(push_in):
return GetPopOut_All([[]], push_in, [])
def GetPopOut_All(old_possible_list,push_value,curren_stack):
from __builtin__ import len
if len(push_value)==0:
templist=[]
while curren_stack:
templist.append(curren_stack.pop(-1))
new_possible_list=copy.deepcopy(old_possible_list)
for inner_list in new_possible_list:
inner_list.extend(templist)
return new_possible_list
else:
final_re=[]
assert type(old_possible_list[0])==list,"Error with list"
if len(curren_stack)!=0:
tostack_newlist=copy.deepcopy(old_possible_list)
tostack_newstack=curren_stack[:]
tostack_newpush=push_value[:]
popedvalue=tostack_newstack.pop(-1)
for innerlist in tostack_newlist:
innerlist.append(popedvalue)
tostack_re=GetPopOut_All(tostack_newlist, tostack_newpush, tostack_newstack)
final_re.extend(tostack_re)
push_to_stack_list=copy.deepcopy(old_possible_list)
pushtostack_newstack=curren_stack[:]
pushtostack_newpush=push_value[:]
pushtostack_newstack.append(pushtostack_newpush.pop(0))
pushtostack_re=GetPopOut_All(push_to_stack_list, pushtostack_newpush, pushtostack_newstack)
final_re.extend(pushtostack_re)
return final_re
def GetStrFromList(inlist):
#from _ast import Str
return ''.join([str(item)for item in inlist])
if __name__ == '__main__':
pushin_list=[1,2,3,4,5]
from __builtin__ import str
strlist=[GetStrFromList(inner_list) for inner_list in CanPopOut(pushin_list)]
ceshi_list=[4,3,5,1,2]
for tempstr in strlist:
print tempstr
print GetStrFromList(ceshi_list) in strlist
def IsSuit(push_list,pop_list,maxlen): inner_stack=[] while push_list: while push_list and (not inner_stack or inner_stack[-1]!=pop_list[0]): inner_stack.append(push_list.pop(0)) while inner_stack and pop_list and inner_stack[-1]==pop_list[0]: inner_stack.pop(-1) pop_list.pop(0) return len(pop_list)==0看了答案改的。确实我是想不到的。。
相关文章推荐
- 每天一道算法题(11)——栈的push、pop 序列
- 微软面试100题之29题:栈的push、pop 序列
- 栈的push、pop序列
- 栈的push、pop序列
- 栈的push、pop序列
- 29.栈的push、pop序列(栈)
- 栈的push、pop 序列
- 栈的push、pop序列
- 【100题】第二十九 栈的push、pop序列
- 程序员面试题精选100题(24)-栈的push、pop序列[数据结构]
- 25. 微软面试题:栈的push、pop序列(栈)
- 每天一道算法题(11)——栈的push、pop 序列
- 栈的push、pop序列
- 题目:输入两个整数序列。其中一表示栈的 push 顺序,判断另一个序列有没可能是对应的 pop 顺序
- 程序员面试题精选(24):栈的push、pop序列
- 29.栈的push、pop 序列
- 根据栈的入栈序列和出栈序列,打印push和pop的顺序-Amazon笔试题
- 栈的push、pop序列(29)
- 栈的push、pop序列
- 微软算法100道题-------输入两个整数序列。其中一个序列表示栈的push顺序, 判断另一个序列有没有可能是对应的pop顺序。