码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]Decode String

时间:2020-02-09 00:46:10      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:str   stack   elf   class   for   pytho   string   sel   des   

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

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

标签:str   stack   elf   class   for   pytho   string   sel   des   

原文地址:https://www.cnblogs.com/lautsie/p/12285853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!