当前位置:网站首页>241. Design priorities for operational expressions
241. Design priorities for operational expressions
2022-07-01 14:23:00 【anieoo】
solution:
recursive + Divide and conquer
① Recursively save all the combinations of numbers generated on the left and right of the symbol
② Calculate the number combination of each layer through the return value
③ Recursively, only numbers are saved directly
class Solution {
public:
vector<int> diffWaysToCompute(string expression) {
vector<int> res; // Define the return value
for(int i = 0;i < expression.size();i++) {
char c = expression[i];
if(c == '+' || c == '-' || c == '*') {
auto res1 = diffWaysToCompute(expression.substr(0, i)); // Recursive decomposition
auto res2 = diffWaysToCompute(expression.substr(i + 1));
// Yes res1 and res2 Conduct calculation results
for(auto &x : res1)
for(auto &y : res2) {
if(c == '+') res.push_back(x + y);
else if(c == '-') res.push_back(x - y);
else if(c == '*') res.push_back(x * y);
}
}
}
// Recursion to the innermost layer , Save numbers directly
if(res.empty()){
res.push_back(stoi(expression));
}
return res;
}
};
边栏推荐
猜你喜欢
Sqlachemy common operations
Scheme of printing statistical information in log
使用 Lambda 函数URL + CloudFront 实现S3镜像回源
That hard-working student failed the college entrance examination... Don't panic! You have another chance to counter attack!
WebSocket(简单体验版)
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?
Introduction to distributed transactions (Seata)
phpcms实现订单直接支付宝支付功能
leetcode622.设计循环队列(C语言)
随机推荐
Leetcode(69)——x 的平方根
What problems should be considered for outdoor LED display?
leetcode622.设计循环队列(C语言)
队列的基本操作(C语言实现)
Buuctf reinforcement question ezsql
逻辑是个好东西
SWT/ANR问题--当发送ANR/SWT时候如何打开binder trace(BinderTraces)
In depth cooperation | Taosi data cooperates with changhongjia Huawei customers in China to provide tdengine with powerful enterprise level products and perfect service guarantee
Build your own website (21)
算网融合赋能行业转型,移动云点亮数智未来新路标
Research Report on the development trend and competitive strategy of the global aviation leasing service industry
Basis of target detection (NMS)
Distributed dynamic (collaborative) rendering / function runtime based on computing power driven, data and function collaboration
力扣解法汇总241-为运算表达式设计优先级
phpcms实现订单直接支付宝支付功能
户外LED显示屏应该考虑哪些问题?
Research Report on the development trend and competitive strategy of the global ultrasonic scalpel system industry
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
[IOT design. Part I] stm32+ smart cloud aiot+ laboratory security monitoring system
Fiori applications are shared through the enhancement of adaptation project