题解

栈的压入、弹出序列

http://www.nowcoder.com/questionTerminal/d77d11405cc7470d82554cb392585106

python 实现。自己的一点小思路,欢迎指正。主要利用了list中的元素互不相同的特性

class Solution:
    def IsPopOrder(self, pushV, popV):
        # write code here
        if not pushV and not popV:
            return True
        if not pushV or not popV:
            return False

        popV_copy=popV[:]
        for i in pushV:
            if i not in popV_copy:
                return False
            else:
                popV_copy.remove(i)

        start = popV[0]
        start_index = pushV.index(start)
        for i in range(start_index):
            if popV.index(pushV[i])<popV.index(pushV[i+1]):
                return False
        return True
全部评论

相关推荐

04-14 20:10
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
自由水:笑死了,敢这么面试不敢让别人说
点赞 评论 收藏
分享
今天 22:17
北京大学
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务