[leetcode]Decode String

Posted 阿牧遥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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

  

以上是关于[leetcode]Decode String的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode - Decode String

Leetcode -- 394. Decode String

Leetcode 394: Decode String

LeetCode-Decode String

LeetCode-394. Decode String(DFS)

[LeetCode] Decode String 解码字符串