当前位置:网站首页>241. 为运算表达式设计优先级
241. 为运算表达式设计优先级
2022-07-01 14:20:00 【anieoo】

solution:
递归+分治
① 递归保存符号左边和右边产生的所有数字组合方式
② 通过返回值计算每一层的数字组合
③ 递归的到只有数字直接保存
class Solution {
public:
vector<int> diffWaysToCompute(string expression) {
vector<int> res; //定义返回值
for(int i = 0;i < expression.size();i++) {
char c = expression[i];
if(c == '+' || c == '-' || c == '*') {
auto res1 = diffWaysToCompute(expression.substr(0, i)); //递归分解
auto res2 = diffWaysToCompute(expression.substr(i + 1));
//对res1和res2进行计算结果
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);
}
}
}
//递归到最里面一层,直接保存数字
if(res.empty()){
res.push_back(stoi(expression));
}
return res;
}
};边栏推荐
- 2022 PMP project management examination agile knowledge points (6)
- 光环效应——谁说头上有光的就算英雄
- Phpcms realizes the direct Alipay payment function of orders
- App automation testing Kaiyuan platform appium runner
- Research Report on the development trend and competitive strategy of the global electromagnetic flowmeter industry
- Summary of leetcode's dynamic programming 5
- Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
- When the main process architecture game, to prevent calls everywhere to reduce coupling, how to open the interface to others to call?
- 30 Devops interview questions and answers
- After being laid off for three months, the interview ran into a wall everywhere, and the mentality has begun to collapse
猜你喜欢

原来程序员搞私活这么赚钱?真的太香了

Today, with the popularity of micro services, how does service mesh exist?

"National defense seven sons" funding soared, with Tsinghua reaching 36.2 billion yuan, ranking second with 10.1 billion yuan. The 2022 budget of national colleges and universities was made public

After being laid off for three months, the interview ran into a wall everywhere, and the mentality has begun to collapse

2022 PMP project management examination agile knowledge points (6)

QT learning management system

leetcode622.设计循环队列(C语言)

Details of appium key knowledge

如何看待国企纷纷卸载微软Office改用金山WPS?

【R语言数据科学】:机器学习常见评估指标
随机推荐
Details of appium key knowledge
【Flask】Flask启程与实现一个基于Flask的最小应用程序
Logic is a good thing
使用 Lambda 函数URL + CloudFront 实现S3镜像回源
程序设计的基本概念
Leetcode (69) -- square root of X
Fiori applications are shared through the enhancement of adaptation project
Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
sqlilabs less-8
leetcode622. Design cycle queue (C language)
Oracle-数据库对象的使用
Après avoir été licencié pendant trois mois, l'entrevue s'est effondrée et l'état d'esprit a commencé à s'effondrer.
Leetcode(69)——x 的平方根
2022年PMP项目管理考试敏捷知识点(6)
What problems should be considered for outdoor LED display?
Introduction to distributed transactions (Seata)
Research Report on the development trend and competitive strategy of the global commercial glassware industry
日志中打印统计信息的方案
【IoT毕设.上】STM32+机智云AIoT+实验室安全监控系统
One of the data Lake series | you must love to read the history of minimalist data platforms, from data warehouse, data lake to Lake warehouse