Leetcode 1807. Evaluate the Bracket Pairs of a String
Posted SnailTyan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 1807. Evaluate the Bracket Pairs of a String相关的知识,希望对你有一定的参考价值。
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
**解析:**Version 1,遍历字符串,找到所有括号内的字符串,替换即可。
- Version 1
class Solution:
def evaluate(self, s: str, knowledge: List[List[str]]) -> str:
knowledge = {x[0]: x[1] for x in knowledge}
n = len(s)
i = 0
result = ''
while i < n:
if s[i] == '(':
key = ''
i += 1
while s[i] != ')':
key += s[i]
i += 1
if key in knowledge:
result += knowledge[key]
else:
result += '?'
else:
result += s[i]
i += 1
return result
Reference
以上是关于Leetcode 1807. Evaluate the Bracket Pairs of a String的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode 150. Evaluate Reverse Polish Notation
LeetCode 399. Evaluate Division