当前位置:网站首页>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;
}
};边栏推荐
- [IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
- Research Report on the development trend and competitive strategy of the global display filter industry
- Après avoir été licencié pendant trois mois, l'entrevue s'est effondrée et l'état d'esprit a commencé à s'effondrer.
- Today, with the popularity of micro services, how does service mesh exist?
- 玩转gRPC—不同编程语言间通信
- [commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
- Research Report on development trend and competitive strategy of global consumer glassware industry
- Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
- 户外LED显示屏应该考虑哪些问题?
- 佩服,阿里女程序卧底 500 多个黑产群……
猜你喜欢

How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?

【IoT毕设.下】STM32+机智云AIoT+实验室安全监控系统

sqlilabs less13

玩转gRPC—不同编程语言间通信

Six years of technology iteration, challenges and exploration of Alibaba's globalization and compliance

WebSocket(简单体验版)

Fiori applications are shared through the enhancement of adaptation project

2022. Let me take you from getting started to mastering jetpack architecture components - lifecycle

【牛客网刷题系列 之 Verilog快速入门】~ 使用函数实现数据大小端转换

【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
随机推荐
Logic is a good thing
使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
用栈实现队列、用队列实现栈(C语言_leetcode_232+225)
既不是研发顶尖高手,也不是销售大牛,为何偏偏获得 2 万 RMB 的首个涛思文化奖?
2022 PMP project management examination agile knowledge points (6)
Realize queue with stack and stack with queue (C language \leetcode\u 232+225)
sqlilabs less10
建立自己的网站(21)
Research Report on the development trend and competitive strategy of the global pipeline robot inspection camera industry
Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
[IOT design. Part I] stm32+ smart cloud aiot+ laboratory security monitoring system
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?
Research Report on the development trend and competitive strategy of the global axis measurement system industry
App自动化测试开元平台Appium-runner
WebSocket(简单体验版)
Websocket (simple experience version)
Leetcode(69)——x 的平方根
Research Report on the development trend and competitive strategy of the global traditional computer industry
Open source internship experience sharing: openeuler software package reinforcement test