当前位置:网站首页>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/
边栏推荐
- Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
- How do I use Google Chrome 11's Upload Folder feature in my own code?
- [TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
- You cannot right-click F12 to view the source code solution on the web page
- Use of comment keyword in database
- Gorilla/mux framework (RK boot): RPC error code design
- 205. 同构字符串
- Cookie&Session
- 在线公网安备案保姆级教程【伸手党福利】
- LeetCode 128最长连续序列(哈希set)
猜你喜欢

Review column - message queue

【TA-霜狼_may-《百人计划》】1.4 PC手机图形API介绍

Edlines: a real time line segment detector with a false detection control

Error: plug ins declaring extensions or extension points must set the singleton directive to true

Ridge regression and lasso regression

Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page

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

【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹

pytorch nn. AdaptiveAvgPool2d(1)

不用加减乘除实现加法
随机推荐
10、Scanner.next() 无法读取空格/indexOf -1
389. 找不同
Blueprism registration, download and install -rpa Chapter 1
Avalanche problem and the use of sentinel
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
Appium自动化测试基础--补充:C/S架构和B/S架构说明
10. 正则表达式匹配
Future of NTF and trends in 2022
JS daily development tips (continuous update)
Bilinear upsampling and f.upsample in pytorch_ bilinear
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
Research on target recognition and tracking based on 3D laser point cloud
后台系统右边内容如何出现滚动条和解决双滚动条的问题
BluePrism注册下载并安装-RPA第一章
8. 字符串转换整数 (atoi)
【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
【TA-霜狼_may-《百人计划》】2.1 色彩空间
Promql select time series
【伸手党福利】JSONObject转String保留空字段