题解 | #牛牛的字符串解码问题#

牛牛的字符串解码问题

https://www.nowcoder.com/practice/e5658311e6d44b74872e843ba13ee290

#coding:utf-8
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @return string字符串
#
class Solution:
    def decodeString(self , s ):
        # write code here
        stack = []
        current_str = ""
        current_num = 0
    
        for char in s:
            if char.isdigit():#查看一个字符是否为数字
                current_num = current_num * 10 + int(char)
            elif char == '[':
                stack.append((current_str, current_num))
                current_str = ""
                current_num = 0
            elif char == ']':
                prev_str, num = stack.pop()
                current_str = prev_str + current_str * num
            else:
                current_str += char
    
        return current_str

全部评论

相关推荐

评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务