题解 | #牛牛的字符串解码问题#
牛牛的字符串解码问题
https://www.nowcoder.com/practice/e5658311e6d44b74872e843ba13ee290
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param s string字符串
# @return string字符串
#
class Solution:
def decodeString(self , s: str) -> str:
# write code here
# replace the origin string to eliminate "["
while "[" in s:
# process the last "[" in the current string to deal with [[]]
for i in range(len(s)-1,0,-1):
if s[i] == "[":
for j in range(i+1,len(s)):
if s[j] == "]":
# replace s by simply adding
s = s[:i-1]+int(s[i-1])*s[i+1:j]+s[j+1:]
break
return s