思路:类似于入栈出栈的操作,分层保存数字和字符串,然后逐层相乘合并,通过判断当前字符和上一个字符类型来确定数字是否结束
class Solution:def decodeString(self, s: str) -> str:current_str = [""]current_num=[]last_char=""for each_char in s:if each_char.isalpha():current_str[-1] += each_charelif each_char.isnumeric():if not last_char.isnumeric():current_num.append(0)current_num[-1] = current_num[-1] * 10 + int(each_char)elif each_char == '[':current_str.append("")elif each_char == ']':current_str[-2]+= current_str[-1] * current_num[-1]current_str.pop(-1)current_num.pop(-1)last_char=each_charwhile current_num:print(current_str, current_num)current_str[-2]+=current_str[-1]*current_num[-1]current_str.pop(-1)current_num.pop(-1)print("current",current_str)return current_str[-1]