当前位置:网站首页>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;
}
};边栏推荐
- C language ordering management system
- 自定义注解实现验证信息的功能
- [NLP] pre training model - gpt1
- How to pass array parameters in get request
- Fiori applications are shared through the enhancement of adaptation project
- 被裁三个月,面试到处碰壁,心态已经开始崩了
- SWT/ANR问题--如何捕获性能的trace
- 2022 PMP project management examination agile knowledge points (6)
- 【NLP】预训练模型——GPT1
- 【IoT毕设.下】STM32+机智云AIoT+实验室安全监控系统
猜你喜欢

用栈实现队列、用队列实现栈(C语言_leetcode_232+225)

Tdengine connector goes online Google Data Studio app store

App automation testing Kaiyuan platform appium runner

2022年PMP项目管理考试敏捷知识点(6)

App自动化测试开元平台Appium-runner

【R语言数据科学】:机器学习常见评估指标

【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程

sqlilabs less-8

Oracle-数据库对象的使用

Fiori applications are shared through the enhancement of adaptation project
随机推荐
【Flask】Flask启程与实现一个基于Flask的最小应用程序
券商万1免5证券开户是合理安全的吗,怎么讲
TDengine 连接器上线 Google Data Studio 应用商店
Go整合Logrus实现日志打印
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
【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较
2022年PMP项目管理考试敏捷知识点(6)
当主程架构游戏的时候,防止到处调用减少耦合性,怎么开放接口给其他人调用呢?
Use of Oracle database objects
力扣解法汇总241-为运算表达式设计优先级
C language ordering management system
数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
Tdengine connector goes online Google Data Studio app store
玩转MongoDB—搭建MongoDB集群
[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
phpcms实现订单直接支付宝支付功能
【IoT毕设.下】STM32+机智云AIoT+实验室安全监控系统
[flask] flask starts and implements a minimal application based on flask
Distributed dynamic (collaborative) rendering / function runtime based on computing power driven, data and function collaboration
sqlilabs less9