当前位置:网站首页>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;
}
};
边栏推荐
- Multiprocess programming (V): semaphores
- Overlay of shutter (Pop-Up)
- Arduino开发之按键检测与正弦信号输出
- Some introduction and precautions about XML
- Thread start and priority
- Machine learning: numpy version linear regression predicts Boston house prices
- Rust string slicing, structs, and enumeration classes
- leetcode-871:最低加油次数
- Vulkan并非“灵药“
- Nc17059 queue Q
猜你喜欢

Rust ownership (very important)

如何系统学习机器学习

可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场

Is there a free text to speech tool to help recommend?

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

AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight

Automated defect analysis in electronic microscopic images

Detailed explanation of pod life cycle

Vulkan-实践第一弹

1.11 - 总线
随机推荐
Vulkan并非“灵药“
Multiprocess programming (II): Pipeline
百数不断创新,打造自由的低代码办公工具
字符设备注册常用的两种方法和步骤
leetcode-934:最短的桥
Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
【AutoSAR 十一 通信相关机制】
ftrace工具的介绍及使用
logback配置文件
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
Detailed explanation of pod life cycle
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
How to systematically learn machine learning
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
An excellent orm in dotnet circle -- FreeSQL
cordova-plugin-device获取设备信息插件导致华为审核不通过
Free we media essential tools sharing
Redis21 classic interview questions, extreme pull interviewer
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
kubernetes编写yml简单入门