当前位置:网站首页>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
边栏推荐
- localtime居然不可重入,踩坑了
- AI抠图工具
- Ipv6-6to4 experiment
- 6.30 simulation summary
- Digital signal processing -- Design of linear phase (Ⅱ, Ⅳ) FIR filter (2)
- R语言基于h2o包构建二分类模型:使用h2o.gbm构建梯度提升机模型GBM、使用h2o.auc计算模型的AUC值
- Operations related to sequence table
- 队列的链式存储
- Blue Bridge Cup multi interface switching processing (enumeration plus state machine method)
- [some notes]
猜你喜欢

【语音信号处理】3语音信号可视化——prosody

Indefinite integral

《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现

MySQL common functions
![[JS] interview questions](/img/f3/8cf430b999980190a250f89537715e.jpg)
[JS] interview questions

2022-06-28-06-29

Switch basic experiment
![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/](/img/6a/fe448ca635690bc5260436546b588e.jpg)
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/

Queue operation---

项目部署,一点也不难!
随机推荐
ustime写出了bug
编译调试Net6源码
How to install php7 and perform performance test using yum
第十四章 信号(四)- 多进程任务示例
JPA and criteria API - select only specific columns - JPA & criteria API - select only specific columns
R语言使用yardstick包的conf_mat函数计算多分类(Multiclass)模型在每个交叉验证(或者重采样)的每一折fold上的混淆矩阵、并使用summary输出每个fold的其它详细指标
Sort out relevant contents of ansible
redis探索之缓存击穿、缓存雪崩、缓存穿透
JS related interview questions and answers (1)
Ansible的playbook
華為面試題: 招聘
JS reverse | m3u8 data decryption of a spring and autumn network
(混更一篇)多个txt文本转一个表格
本科毕业四年:工作,辞职,结婚,买房
【邂逅Django】——(二)数据库配置
[speech signal processing] 3 speech signal visualization -- prosody
使用BurpSuite对app抓包教程
Ipv6-6to4 experiment
Eurake partition understanding
GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021