当前位置:网站首页>241. Design priorities for operational expressions
241. Design priorities for operational expressions
2022-07-01 03:43:00 【Sun_ Sky_ Sea】
241. Design priorities for operation expressions
Original title link :https://leetcode.cn/problems/different-ways-to-add-parentheses/
Give you a string of numbers and operators expression , Combine numbers and operators according to different priorities , Calculate and return the results of all possible combinations . You can In any order Return to the answer .
Example 1:
Input :expression = “2-1-1”
Output :[0,2]
explain :
((2-1)-1) = 0
(2-(1-1)) = 2
Example 2:
Input :expression = “23-45”
Output :[-34,-14,-10,-10,10]
explain :
(2*(3-(45))) = -34
((23)-(45)) = -14
((2(3-4))5) = -10
(2((3-4)5)) = -10
(((23)-4)*5) = 10
Tips :
1 <= expression.length <= 20
expression By numbers and operators ‘+’、‘-’ and ‘*’ form .
All integer values in the input expression are in the range [0, 99]
Their thinking :
Divide and conquer algorithm , Bounded by operators , Divided into left and right subexpressions , I'll go on , Until it's a number ( End of recursion flag ), Then judge the operator to merge .
Code implementation :
class Solution:
def diffWaysToCompute(self, expression: str) -> List[int]:
# End of recursion flag : Only numbers , Return the number directly
if expression.isdigit():
return [int(expression)]
ans = []
for i, c in enumerate(expression):
# Split when encountering operators , Calculate the values of the left and right subexpressions respectively
if c in "+-*":
left = self.diffWaysToCompute(expression[:i])
right = self.diffWaysToCompute(expression[i + 1:])
# Combine the values of the numerator expression
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
reference :
https://leetcode.cn/problems/different-ways-to-add-parentheses/solution/pythongolang-fen-zhi-suan-fa-by-jalan/
边栏推荐
- Binary tree god level traversal: Morris traversal
- Develop industrial Internet with the technical advantages of small programs
- 【TA-霜狼_may-《百人计划》】2.4 传统经验光照模型
- LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
- [daily training] 1175 Prime permutation
- 用小程序的技术优势发展产业互联网
- 5. [WebGIS practice] software operation - service release and permission management
- 【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
- 4、【WebGIS实战】软件操作篇——数据导入及处理
- 谷粒学院微信扫码登录过程记录以及bug解决
猜你喜欢
【TA-霜狼_may-《百人计划》】1.4 PC手机图形API介绍
jeecgboot输出日志,@Slf4j的使用方法
Leetcode 128 longest continuous sequence (hash set)
LeetCode 128最长连续序列(哈希set)
Error: plug ins declaring extensions or extension points must set the singleton directive to true
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
随机推荐
快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
不用加减乘除实现加法
389. 找不同
Thread data sharing and security -threadlocal
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
The combination of applet container technology and IOT
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
ECMAScript 6.0
Binary tree god level traversal: Morris traversal
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
静态库使用MFC和共享库使用MFC的区别
Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
392. 判断子序列
Idea plug-in backup table
Edlines: a real time line segment detector with a false detection control
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation