当前位置:网站首页>241. 为运算表达式设计优先级
241. 为运算表达式设计优先级
2022-07-01 03:23:00 【Sun_Sky_Sea】
241. 为运算表达式设计优先级
原始题目链接:https://leetcode.cn/problems/different-ways-to-add-parentheses/
给你一个由数字和运算符组成的字符串 expression ,按不同优先级组合数字和运算符,计算并返回所有可能组合的结果。你可以 按任意顺序 返回答案。
示例 1:
输入:expression = “2-1-1”
输出:[0,2]
解释:
((2-1)-1) = 0
(2-(1-1)) = 2
示例 2:
输入:expression = “23-45”
输出:[-34,-14,-10,-10,10]
解释:
(2*(3-(45))) = -34
((23)-(45)) = -14
((2(3-4))5) = -10
(2((3-4)5)) = -10
(((23)-4)*5) = 10
提示:
1 <= expression.length <= 20
expression 由数字和算符 ‘+’、‘-’ 和 ‘*’ 组成。
输入表达式中的所有整数值在范围 [0, 99]
解题思路:
分治算法,以运算符为界限,分成左右两边子表达式,再继续分,直到是数字(递归结束标志),再判断运算符进行合并。
代码实现:
class Solution:
def diffWaysToCompute(self, expression: str) -> List[int]:
# 递归结束标志:只有数字,直接返回数字
if expression.isdigit():
return [int(expression)]
ans = []
for i, c in enumerate(expression):
# 遇到运算符就拆分,分别计算左右两侧的子表达式的值
if c in "+-*":
left = self.diffWaysToCompute(expression[:i])
right = self.diffWaysToCompute(expression[i + 1:])
# 合并拆分子表达式的值
for l in left:
for r in right:
if c == '+':
ans.append(l + r)
if c == '-':
ans.append(l - r)
if c == '*':
ans.append(l * r)
return ans
参考文献:
https://leetcode.cn/problems/different-ways-to-add-parentheses/solution/pythongolang-fen-zhi-suan-fa-by-jalan/
边栏推荐
- IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
- Data exchange JSON
- Golang multi graph generation gif
- How do I use Google Chrome 11's Upload Folder feature in my own code?
- 5、【WebGIS实战】软件操作篇——服务发布及权限管理
- 深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
- 使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
- Pyramid Scene Parsing Network【PSPNet】论文阅读
- Ouc2021 autumn - Software Engineering - end of term (recall version)
- Pathmeasure implements loading animation
猜你喜欢

排序链表(归并排序)

Asgnet paper and code interpretation 2

The difference between MFC for static libraries and MFC for shared libraries

The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list

家居网购项目

Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)

Listener listener

bootsrap中的栅格系统

TEC: Knowledge Graph Embedding with Triple Context

后台系统右边内容如何出现滚动条和解决双滚动条的问题
随机推荐
Learning notes for introduction to C language multithreaded programming
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
文件上传下载
torch. histc
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
实现pow(x,n)函数
leetcode 1482 猜猜看啊,这道题目怎么二分?
Server rendering technology JSP
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
Test function in pychram
Ridge regression and lasso regression
Overview of EtherCAT principle
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
在线公网安备案保姆级教程【伸手党福利】
You cannot right-click F12 to view the source code solution on the web page
Jeecgboot output log, how to use @slf4j
Detailed explanation of ES6 deconstruction grammar
Ultimate dolls 2.0 | encapsulation of cloud native delivery
ES6解构语法详解
GCC usage, makefile summary