当前位置:网站首页>【241. 为运算表达式设计优先级】
【241. 为运算表达式设计优先级】
2022-07-01 13:19:00 【千北@】
来源:力扣(LeetCode)
描述:
给你一个由数字和运算符组成的字符串 expression ,按不同优先级组合数字和运算符,计算并返回所有可能组合的结果。你可以 按任意顺序 返回答案。
生成的测试用例满足其对应输出值符合 32 位整数范围,不同结果的数量不超过 104 。
示例 1:
输入:expression = "2-1-1"
输出:[0,2]
解释:
((2-1)-1) = 0
(2-(1-1)) = 2
示例 2:
输入:expression = "2*3-4*5"
输出:[-34,-14,-10,-10,10]
解释:
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
提示:
1 <= expression.length <= 20- expression 由数字和算符
'+'、'-'和'*'组成。 - 输入表达式中的所有整数值在范围 [0, 99]
方法一:记忆化搜索
思路与算法

代码:
class Solution {
public:
const int ADDITION = -1;
const int SUBTRACTION = -2;
const int MULTIPLICATION = -3;
vector<int> dfs(vector<vector<vector<int>>>& dp, int l, int r, const vector<int>& ops) {
if (dp[l][r].empty()) {
if (l == r) {
dp[l][r].push_back(ops[l]);
} else {
for (int i = l; i < r; i += 2) {
auto left = dfs(dp, l, i, ops);
auto right = dfs(dp, i + 2, r, ops);
for (auto& lv : left) {
for (auto& rv : right) {
if (ops[i + 1] == ADDITION) {
dp[l][r].push_back(lv + rv);
} else if (ops[i + 1] == SUBTRACTION) {
dp[l][r].push_back(lv - rv);
} else {
dp[l][r].push_back(lv * rv);
}
}
}
}
}
}
return dp[l][r];
}
vector<int> diffWaysToCompute(string expression) {
vector<int> ops;
for (int i = 0; i < expression.size();) {
if (!isdigit(expression[i])) {
if (expression[i] == '+') {
ops.push_back(ADDITION);
} else if (expression[i] == '-') {
ops.push_back(SUBTRACTION);
} else {
ops.push_back(MULTIPLICATION);
}
i++;
} else {
int t = 0;
while (i < expression.size() && isdigit(expression[i])) {
t = t * 10 + expression[i] - '0';
i++;
}
ops.push_back(t);
}
}
vector<vector<vector<int>>> dp((int) ops.size(), vector<vector<int>>((int) ops.size()));
return dfs(dp, 0, ops.size() - 1, ops);
}
};
执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
内存消耗:7 MB, 在所有 C++ 提交中击败了85.25%的用户
复杂度分析
时间复杂度:O(2n),其中 n 为 ops 的大小。
空间复杂度:O(2n),其中 n 为 ops 的大小。
方法二:动态规划
思路与算法

代码:
class Solution {
public:
const int ADDITION = -1;
const int SUBTRACTION = -2;
const int MULTIPLICATION = -3;
vector<int> diffWaysToCompute(string expression) {
vector<int> ops;
for (int i = 0; i < expression.size();) {
if (!isdigit(expression[i])) {
if (expression[i] == '+') {
ops.push_back(ADDITION);
} else if (expression[i] == '-') {
ops.push_back(SUBTRACTION);
} else {
ops.push_back(MULTIPLICATION);
}
i++;
} else {
int t = 0;
while (i < expression.size() && isdigit(expression[i])) {
t = t * 10 + expression[i] - '0';
i++;
}
ops.push_back(t);
}
}
vector<vector<vector<int>>> dp((int) ops.size(), vector<vector<int>>((int) ops.size()));
for (int i = 0; i < ops.size(); i += 2) {
dp[i][i] = {
ops[i]};
}
for (int i = 3; i <= ops.size(); i++) {
for (int j = 0; j + i <= ops.size(); j += 2) {
int l = j;
int r = j + i - 1;
for (int k = j + 1; k < r; k += 2) {
auto& left = dp[l][k - 1];
auto& right = dp[k + 1][r];
for (auto& num1 : left) {
for (auto& num2 : right) {
if (ops[k] == ADDITION) {
dp[l][r].push_back(num1 + num2);
}
else if (ops[k] == SUBTRACTION) {
dp[l][r].push_back(num1 - num2);
}
else if (ops[k] == MULTIPLICATION) {
dp[l][r].push_back(num1 * num2);
}
}
}
}
}
}
return dp[0][(int) ops.size() - 1];
}
};
执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
内存消耗:6.5 MB, 在所有 C++ 提交中击败了92.05%的用户
复杂度分析
时间复杂度:O(2n),其中 n 为 ops 的大小。
空间复杂度:O(2n),其中 n 为 ops 的大小。
author:LeetCode-Solution
边栏推荐
- Analysis report on production and marketing demand and investment forecast of global and Chinese diamond powder industry Ⓤ 2022 ~ 2027
- 内容审计技术
- Reasons for MySQL reporting 1040too many connections and Solutions
- IO的几种模型 阻塞,非阻塞,io多路复用,信号驱动和异步io
- Beidou communication module Beidou GPS module Beidou communication terminal DTU
- Grafana reports an error: error= "failed to send notification to email addresses: [email protected] : 535 Error:
- Example code of second kill based on MySQL optimistic lock
- Global and Chinese n-butanol acetic acid market development trend and prospect forecast report Ⓧ 2022 ~ 2028
- Different test techniques
- Three questions about scientific entrepreneurship: timing, pain points and important decisions
猜你喜欢

Colorful five pointed star SVG dynamic web page background JS special effect

Wave animation color five pointed star loader loading JS special effects
MySQL报错1040Too many connections的原因以及解决方案

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

Feign & Eureka & zuul & hystrix process

5G工业网关的科技治超应用 超限超重超速非现场联合执法

What is the future development direction of people with ordinary education, appearance and family background? The career planning after 00 has been made clear

学历、长相、家境普通的人,未来的发展方向是什么?00后的职业规划都已经整得明明白白......

刘对(火线安全)-多云环境的风险发现

minimum spanning tree
随机推荐
Judea pearl, Turing prize winner: 19 causal inference papers worth reading recently
Detailed explanation of parallel replication examples in MySQL replication
Function test process in software testing
Asp. NETCORE uses dynamic to simplify database access
1.8新特性-List
VM virtual machine configuration dynamic IP and static IP access
详细讲解面试的 IO多路复用,select,poll,epoll
Anti fraud, refusing to gamble, safe payment | there are many online investment scams, so it's impossible to make money like this
China NdYAG crystal market research conclusion and development strategy proposal report Ⓥ 2022 ~ 2028
MySQL 66 questions, 20000 words + 50 pictures in detail! Necessary for review
Use of shutter SQLite
spark源码阅读总纲
Analysis report on the development prospect and investment strategy of the global and Chinese laser chip industry Ⓑ 2022 ~ 2027
3.4 《数据库系统概论》之数据查询—SELECT(单表查询、连接查询、嵌套查询、集合查询、多表查询)
nexus搭建npm依赖私库
The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
Meta再放大招!VR新模型登CVPR Oral:像人一样「读」懂语音
leetcode 322. Coin change (medium)
Analysis report on the development pattern of China's smart emergency industry and the 14th five year plan Ⓠ 2022 ~ 2028
Leetcode第一题:两数之和(3种语言)