当前位置:网站首页>Leetcode-241: designing priorities for operational expressions
Leetcode-241: designing priorities for operational expressions
2022-07-03 00:47:00 【Chrysanthemum headed bat】
leetcode-241: Design priorities for operation expressions
subject
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 .
The generated test case meets its corresponding output value in line with 32 Bit integer range , The number of different results does not exceed 104 .
Example 1:
Input :expression = "2-1-1"
Output :[0,2]
explain :
((2-1)-1) = 0
(2-(1-1)) = 2
Example 2:
Input :expression = "2*3-4*5"
Output :[-34,-14,-10,-10,10]
explain :
(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
Problem solving
Method 1 :dfs( Divide and conquer )
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);// Divide and conquer : Get the result on the left side of the operator
vector<int> l2=dfs(i+1,r,s);// Divide and conquer : Get the result on the right side of the operator
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;
}
};
边栏推荐
- [MCU project training] eight way answering machine
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- LeedCode1480. Dynamic sum of one-dimensional array
- 线程的启动与优先级
- leetcode-2280:表示一个折线图的最少线段数
- Lex & yacc & bison & flex configuration problems
- 【AutoSAR 八 OS】
- 【AutoSAR 七 工具链简介】
- 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
- About the practice topic of screen related to unity screen, unity moves around a certain point inside
猜你喜欢

File operation io-part2

为什么网站打开速度慢?

Vulkan performance and refinement

2022上半年值得被看见的10条文案,每一句都能带给你力量!

kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)

Why is the website slow to open?

Sentry developer contribution Guide - configure pycharm

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement

【案例分享】让新时代教育发展与“数”俱进
随机推荐
Gan model architecture in mm
文件操作IO-Part2
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
【AutoSAR 九 C/S原理架构】
1.12 - Instructions
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
Linux软件:如何安装Redis服务
JSON conversion tool class
线程的启动与优先级
数组与集合性能比较
Vulkan-实践第一弹
Set up nacos2 X cluster steps and problems encountered
node_ Modules cannot be deleted
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
深度剖析数据在内存中的存储
微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
如何系统学习机器学习
图解网络:什么是虚拟路由器冗余协议 VRRP?
Overlay of shutter (Pop-Up)
Vulkan performance and refinement