我们用栈同时维护当前字符串和倍数以及要加倍的字符串
当遇到"["时,我们保存当前字符串,即将当前字符 cres 串入栈; 当遇到"]"时,res = cres + 倍数 * 应加倍的字符串
class Solution: def decodeString(self, s: str) -> str: stack, res, multi = [], "", 0 for ch in s: if ch == "[": //保存当前字符串和当前倍数 stack.append([multi, res]) res, multi = "", 0 elif ch == "]": curm, cres = stack.pop() res = cres + curm * res //之前保存的字符串 + 应加倍的字符串 * 倍数 elif "0" <= ch <= "9": multi = multi * 10 + int(ch) else: res += ch return res
标签:multi,ch,--,res,cres,394,字符串,stack From: https://www.cnblogs.com/zk6696/p/17679337.html