高级农民
- 积分
- 2526
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-11-26
- 最后登录
- 1970-1-1
|
本帖最后由 MrTian 于 2021-8-24 19:17 编辑
这个代码块功能有亿点点难用,直接粘贴代码了
楼主不清楚的话可以逐行debug或者再评论问
def decodeString(s):
n = len(s)
stack = []
idx = 0
while idx < n:
if s[idx] == "(":
stack.append("(")
elif s[idx] == ")":
# get the number in {}
num = 0
while idx < n and s[idx] != "}":
if s[idx].isdigit():
num = num * 10 + int(s[idx])
idx += 1
# get the repeating string
repeat = ""
while stack[-1] != "(":
repeat += stack.pop()
repeat = repeat[::-1]
# pop placeholder "("
stack.pop()
for char in repeat * num:
stack.append(char)
else:
stack.append(s[idx])
idx += 1
return "".join(stack)
|
|