题目
代码
执行用时:32 ms, 在所有 Python3 提交中击败了82.54% 的用户
内存消耗:14.9 MB, 在所有 Python3 提交中击败了65.53% 的用户
通过测试用例:105 / 105
class Solution:
def interpret(self, command: str) -> str:
command=command.replace("()","o")
command=command.replace("(al)","al")
return command
【方法2】
执行用时:40 ms, 在所有 Python3 提交中击败了33.56% 的用户
内存消耗:14.9 MB, 在所有 Python3 提交中击败了73.47% 的用户
通过测试用例:105 / 105
class Solution:
def interpret(self, command: str) -> str:
ans=""
length=len(command)
idx=0
while idx<length:
if command[idx]=='G':
ans+="G"
idx+=1
elif command[idx]=="(" and command[idx+1]==')':
idx+=2
ans+="o"
else:
idx+=4
ans+="al"
return ans

该博客介绍了两种不同的方法来解释和执行给定的命令字符串。方法1通过直接替换模式匹配来简化命令,而方法2使用迭代和条件判断实现。文章讨论了这两种方法的执行时间和内存消耗,展示了在处理字符串解析时的不同策略。

被折叠的 条评论
为什么被折叠?



