当前位置:网站首页>leetcode:241. Design priority for operation expression [DFS + Eval]
leetcode:241. Design priority for operation expression [DFS + Eval]
2022-07-01 12:36:00 【White speed Dragon King's review】

analysis
Choose one of them every time a ? b Add a pair of parentheses
Know to run out of all operators
Then put in a set It is enough to prevent the expressions from being equal
ac code
class Solution:
def diffWaysToCompute(self, expression: str) -> List[int]:
ans = []
operators = ('+', '-', '*')
def countOperator(s):
cnt = 0
for c in s:
if c in operators:
cnt += 1
return cnt
s = set()
def dfs(nums, ops):
if len(nums) == 1:
s.add(nums[0])
return
# Choose one to calculate
for i in range(len(nums) - 1):
new_num = '(' + nums[i] + ops[i] + nums[i + 1] + ')'
new_nums = nums[:i] + [new_num] + nums[i + 2:]
new_ops = ops[:i] + ops[i + 1:]
dfs(new_nums, new_ops)
# pattern To press ascii code
nums = re.split('[*+-]', expression)
ops = []
for c in expression:
if c in operators:
ops.append(c)
dfs(nums, ops)
return [eval(exp) for exp in s]
summary
dfs Choose the place you can choose
re.split To distinguish between symbols and numbers , Mainly based on ascii The order of the codes
And then finally through eval Calculate the results of all the different formulas
边栏推荐
- List of QT players [easy to understand]
- Question d'entrevue de Huawei: recrutement
- Double linked list related operations
- AI matting tool
- 第十四章 信号(四)- 多进程任务示例
- 【MAUI】为 Label、Image 等控件添加点击事件
- Wechat applet reports an error: [rendering layer network layer error] pages/main/main Local resource pictures in wxss cannot be obtained through wxss. You can use network pictures, Base64, or < image/
- (混更一篇)多个txt文本转一个表格
- GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021
- Ansible相关内容梳理
猜你喜欢

晓看天色暮看云,美图欣赏
![[brain opening] west tide and going to the world series](/img/b2/444af296e170d19629800b3d4c50fa.jpg)
[brain opening] west tide and going to the world series

How to install php7 and perform performance test using yum

队列的链式存储
![[speech signal processing] 3 speech signal visualization -- prosody](/img/06/5f57f9dfe3a0f2f70022706f7d4d17.jpg)
[speech signal processing] 3 speech signal visualization -- prosody
![[20211129] jupyter notebook remote server configuration](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] jupyter notebook remote server configuration
![[Suanli network] technological innovation of Suanli Network -- key technology of operation service](/img/80/6e3648c88d309516d4bc29db9c153c.jpg)
[Suanli network] technological innovation of Suanli Network -- key technology of operation service
![[20211129] configuration du serveur distant du carnet de notes jupyter](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] configuration du serveur distant du carnet de notes jupyter

Switch basic experiment
![[JS] interview questions](/img/f3/8cf430b999980190a250f89537715e.jpg)
[JS] interview questions
随机推荐
Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】
How to install php7 and perform performance test using yum
Ansi/ul 94 VTM vertical burning test for thin materials
【邂逅Django】——(二)数据库配置
[shell programming] - shell introductory learning
Mobile note application
Fatal error: execution: there is no such file or directory
Four years after graduation: work, resign, get married, buy a house
类的初始化与实例化
VS Code 设置代码自动保存
ROS2 Foxy depthai_ros教程
简单斐波那契(递推)
Chapter 14 signals (IV) - examples of multi process tasks
What are the solutions for session sharing of highly paid programmers & interview questions series 118?
"Analysis of 43 cases of MATLAB neural network": Chapter 40 research on prediction of dynamic neural network time series -- implementation of NARX based on MATLAB
GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021
MySQL的零拷贝技术
数字信号处理——线性相位型(Ⅱ、Ⅳ型)FIR滤波器设计(2)
[20211129] jupyter notebook remote server configuration