利用栈排序_python3

用一个栈实现另一个栈的排序

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

开始想的随便写的,100%,6000+ms

m = []
n = []
input()
l = list(map(int, input().split()))
for i in l:
    while m:
        if i >= m[-1]:
            if not n:
                m.append(i)
                break
            while n:
                if n[-1]>=i:
                    m.append(i)
                    break
                else:
                    m.append(n[-1])
                    n.pop(-1)
            else:
                m.append(i)
                break
            break
        else:
            n.append(m[-1])
            m.pop(-1)
    else:
        m.append(i)
        continue
    continue
m.extend(n[::-1])
print(*m[::-1])

优化后71.43%

x = []
input()
l = list(map(int, input().split()))
while l:
    top = l[-1]
    l.pop(-1)
    while x and x[-1] > top:
        l.append(x[-1])
        x.pop(-1)
    x.append(top)
print(*x[::-1])
全部评论

相关推荐

神哥不得了:首先我就是在成都,成都的互联网格外的卷,如果是凭现在的简历的话很难找到大厂,建议再添加一个高质量的项目上去,另外专业技能的话最好是超过每一条的一半
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务