当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
![[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)

Basic use of shell script

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

Shell implements basic file operations (cutting, sorting, and de duplication)

【AutoSAR 四 BSW概述】

【AutoSAR 八 OS】

Hundreds of continuous innovation to create free low code office tools

百数不断创新,打造自由的低代码办公工具

Shell 实现文件基本操作(切割、排序、去重)

【雅思阅读】王希伟阅读P1(阅读判断题)
随机推荐
Program analysis and Optimization - 9 appendix XLA buffer assignment
Nc20806 District interval
antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
Automated defect analysis in electronic microscopic images
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
关于XML一些介绍和注意事项
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
Shell 实现文件基本操作(sed-编辑、awk-匹配)
kubernetes编写yml简单入门
Vulkan practice first bullet
University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
Form form instantiation
Machine learning: numpy version linear regression predicts Boston house prices
【雅思阅读】王希伟阅读P1(阅读判断题)
【AutoSAR 五 方法论】
Hdu3507 (slope DP entry)
Gan model architecture in mm
Shell implements basic file operations (cutting, sorting, and de duplication)
leetcode-849:到最近的人的最大距离