当前位置:网站首页>leetcode-241:为运算表达式设计优先级
leetcode-241:为运算表达式设计优先级
2022-07-02 23:55:00 【菊头蝙蝠】
leetcode-241:为运算表达式设计优先级
题目
给你一个由数字和运算符组成的字符串 expression ,按不同优先级组合数字和运算符,计算并返回所有可能组合的结果。你可以 按任意顺序 返回答案。
生成的测试用例满足其对应输出值符合 32 位整数范围,不同结果的数量不超过 104 。
示例 1:
输入:expression = "2-1-1"
输出:[0,2]
解释:
((2-1)-1) = 0
(2-(1-1)) = 2
示例 2:
输入:expression = "2*3-4*5"
输出:[-34,-14,-10,-10,10]
解释:
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
解题
方法一:dfs(分治)
class Solution {
public:
vector<int> diffWaysToCompute(string expression) {
return dfs(0,expression.size()-1,expression);
}
vector<int> dfs(int l,int r,string& s){
vector<int> res;
for(int i=l;i<=r;i++){
if(s[i]>='0'&&s[i]<='9') continue;
vector<int> l1=dfs(l,i-1,s);//分治:获得运算符左侧结果
vector<int> l2=dfs(i+1,r,s);//分治:获得运算符右侧结果
for(int a:l1){
for(int b:l2){
int cur=0;
if(s[i]=='+') cur=a+b;
else if(s[i]=='-') cur=a-b;
else cur=a*b;
res.push_back(cur);
}
}
}
if(res.empty()){
int num=0;
for(int i=l;i<=r;i++){
num=num*10+s[i]-'0';
}
res.push_back(num);
}
return res;
}
};
边栏推荐
- 2022中国3D视觉企业(引导定位、分拣场景)厂商名单
- Multiprocess programming (I): basic concepts
- LeedCode1480. Dynamic sum of one-dimensional array
- Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
- Win10 多种方式解决无法安装.Net3.5的问题
- The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
- 【AutoSAR 三 RTE概述】
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
- Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
猜你喜欢

【AutoSAR 二 AppL概述】

【AutoSAR 十三 NVM】
![[IELTS reading] Wang Xiwei reading P1 (reading judgment question)](/img/ee/540661fcb2cf1cf1eb15e2026c997a.png)
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)

Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t

Shell 实现文件基本操作(sed-编辑、awk-匹配)

Gan model architecture in mm

文件操作IO-Part2

【AutoSAR 八 OS】

University of Oslo: Li Meng | deep reinforcement learning based on swing transformer

Logback configuration file
随机推荐
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Linux软件:如何安装Redis服务
The most painful programming problem in 2021, adventure of code 2021 Day24
cordova-plugin-device获取设备信息插件导致华为审核不通过
lex && yacc && bison && flex 配置的問題
MySQL 23 classic interview hanging interviewer
Illustrated network: what is virtual router redundancy protocol VRRP?
Briefly talk about other uses of operation and maintenance monitoring
Detailed explanation of pod life cycle
【AutoSAR 九 C/S原理架构】
Multiprocess programming (4): shared memory
mm中的GAN模型架构
数组与集合性能比较
NC24325 [USACO 2012 Mar S]Flowerpot
Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
Vulkan is not a "panacea"“
文件操作IO-Part2
LeedCode1480. Dynamic sum of one-dimensional array
An excellent orm in dotnet circle -- FreeSQL
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度