[leetcode]Decode String

两个栈,永远记录当前解出来的码

class Solution:
    def decodeString(self, s: str) -> str:
        strStack = []
        numStack = []
        current = ''
        num = 0
        for char in s:
            if char >= '0' and char <= '9':
                num = num * 10 + int(char)
            elif char.isalpha():
                current += char
            elif char == '[':
                numStack.append(num)
                strStack.append(current)
                num = 0
                current = ''
            elif char == ']':
                tmpNum = numStack.pop()
                current *= tmpNum
                tmpStr = strStack.pop()
                current = tmpStr + current 

        return current

  

posted @ 2020-02-09 00:14  阿牧遥  阅读(229)  评论(0编辑  收藏  举报