当前位置:网站首页>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
边栏推荐
- 题目 1004: 母牛的故事(递推)
- Good luck brought by years of persistence
- Indefinite integral
- 强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
- 【语音信号处理】3语音信号可视化——prosody
- [datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
- JS reverse | m3u8 data decryption of a spring and autumn network
- Like the three foot platform
- [20211129] jupyter notebook remote server configuration
- Leetcode (Sword finger offer) - 58 - ii Rotate string left
猜你喜欢
![[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
![[20220605] Literature Translation -- visualization in virtual reality: a systematic review](/img/11/6c42957186bf530e8f9d4025a40197.png)
[20220605] Literature Translation -- visualization in virtual reality: a systematic review

【邂逅Django】——(二)数据库配置

Queue operation---

logstash报错:Cannot reload pipeline, because the existing pipeline is not reloadable

MySQL common functions

Share several tools for designing exquisite circuit diagrams

Sleep quality today 79 points

Chained storage of queues

BIM and safety in road maintenance-buildSmart Spain
随机推荐
Friends day 2022
天青色等烟雨
Common chart usage of Bi tools
codeforces -- 4B. Before an Exam
A hole in solder paste
Ansible Playbook
被锡膏坑了一把
Fatal error: execution: there is no such file or directory
ustime写出了bug
[Suanli network] technological innovation of Suanli Network -- key technology of operation service
Application of stack -- bracket matching problem
关于NAND FLASH解扣的认识
VS Code 设置代码自动保存
Teach you to complete the actual battle of image classification hand in hand -- Image Recognition Based on convolutional neural network
循环链表--
【20211129】Jupyter Notebook遠程服務器配置
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
晓看天色暮看云,美图欣赏
[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE
数论基础及其代码实现