【leetcode】394. Decode String
题目如下:
解题思路:这种题目和四则运算,去括号的题目很类似。解法也差不多。
代码如下:
class Solution(object): def decodeString(self, s): """ :type s: str :rtype: str """ stack = [] for i in s: if i != ']': stack.append(i) continue repeatStr = '' while len(stack) > 0: v = stack.pop(-1) if v == '[': break repeatStr = v + repeatStr times = '' while len(stack) > 0: v = stack.pop(-1) if v == ']': break elif v == '[' or (v >= 'a' and v <= 'z'): stack.append(v) break times = v + times repeatStr *= int(times) stack += list(repeatStr) return ''.join(stack)