当前位置:网站首页>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;
}
};
边栏推荐
- After being laid off for three months, the interview ran into a wall everywhere, and the mentality has begun to collapse
- Sorting learning sorting
- 百度上找的期货公司安全吗?期货公司怎么确定正规
- Research Report on the development trend and competitive strategy of the global powder filling machine industry
- 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
- sqlilabs less13
- Fundamentals of C language
- Websocket (simple experience version)
- 开源实习经验分享:openEuler软件包加固测试
- sqlilabs less10
猜你喜欢
【IoT毕设.下】STM32+机智云AIoT+实验室安全监控系统
Scheme of printing statistical information in log
Use the right scene, get twice the result with half the effort! Full introduction to the window query function and usage scenarios of tdengine
The integration of computing and Internet enables the transformation of the industry, and the mobile cloud lights up a new roadmap for the future of digital intelligence
QT学习管理系统
Today, with the popularity of micro services, how does service mesh exist?
So programmers make so much money doing private work? It's really delicious
App automation testing Kaiyuan platform appium runner
Websocket (simple experience version)
[NLP] pre training model - gpt1
随机推荐
原来程序员搞私活这么赚钱?真的太香了
So programmers make so much money doing private work? It's really delicious
That hard-working student failed the college entrance examination... Don't panic! You have another chance to counter attack!
[R language data science]: common evaluation indicators of machine learning
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
App自动化测试开元平台Appium-runner
Halo effect - who says that those with light on their heads are heroes
Leetcode(69)——x 的平方根
Research Report on the development trend and competitive strategy of the global CCTV robot industry
玩转gRPC—不同编程语言间通信
How can we protect our passwords?
How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?
App automation testing Kaiyuan platform appium runner
Research Report on the development trend and competitive strategy of the global diamond suspension industry
Open source internship experience sharing: openeuler software package reinforcement test
C语言基础知识
SWT/ANR问题--如何捕获性能的trace
2022-2-15 learning the imitation Niuke project - Section 3 post details
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
Après avoir été licencié pendant trois mois, l'entrevue s'est effondrée et l'état d'esprit a commencé à s'effondrer.