[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

  

猜你喜欢

转载自www.cnblogs.com/lautsie/p/12285853.html