题解 | #翻转单词序列#

翻转单词序列

http://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3

Python 版本 通过空格识别单词,使用队列存储每个单词,使用栈存储所有单词。

        if not str:
            return None
        astack = []
        aqueue = []
        resstr = ''
        for ind, val in enumerate(str):
            if val == ' ':
                astack.append(aqueue.copy())
                aqueue.clear()
            else:
                aqueue.append(val)
        if aqueue:
            astack.append(aqueue.copy())
        length = len(astack)
        for i in range(length):
            resstr += ''.join(astack.pop())
            if i != length - 1:
                resstr += ' '

        return resstr
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务