当前位置:网站首页>【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
边栏推荐
- 微机原理与接口技术知识点整理复习–纯手打
- 04 redis source code data structure dictionary
- CV顶会最佳论文得主分享:好论文是怎么炼成的?
- Introduction to reverse debugging PE structure input table output table 05/07
- Have you ever encountered the problem that flynk monitors the PostgreSQL database and checkpoints cannot be used
- Global and Chinese silicone defoamer production and marketing demand and investment forecast analysis report Ⓨ 2022 ~ 2027
- nexus搭建npm依赖私库
- A Fletter version of Notepad
- Some summary of pyqt5 learning (overview of the general meaning of some signals and methods)
- 香港科技大学李泽湘教授:我错了,为什么工程意识比上最好的大学都重要?
猜你喜欢

【大型电商项目开发】性能压测-压力测试基本概念&JMeter-38

面试题目总结(1) https中间人攻击,ConcurrentHashMap的原理 ,serialVersionUID常量,redis单线程,

8 popular recommended style layout

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

Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security

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

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

香港科技大学李泽湘教授:我错了,为什么工程意识比上最好的大学都重要?

SAP 智能机器人流程自动化(iRPA)解决方案分享
MySQL报错1040Too many connections的原因以及解决方案
随机推荐
Example code of second kill based on MySQL optimistic lock
Meta再放大招!VR新模型登CVPR Oral:像人一样「读」懂语音
9. Use of better scroll and ref
Report on the "14th five year plan" and scale prospect prediction of China's laser processing equipment manufacturing industry Ⓢ 2022 ~ 2028
【大型电商项目开发】性能压测-压力测试基本概念&JMeter-38
Detailed explanation of OSPF LSA of routing Foundation
During Oracle CDC data transmission, the CLOB type field will lose its value during update. There is a value before update, but
Asp. NETCORE uses dynamic to simplify database access
龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
MySQL Replication中的并行复制示例详解
微机原理与接口技术知识点整理复习–纯手打
minimum spanning tree
1. Sum of two numbers: given an integer array num and an integer target value, please find the two integers whose sum is the target value target in the array and return their array subscripts
1.8新特性-List
彩色五角星SVG动态网页背景js特效
leetcode 322. Coin change (medium)
Analysis report on the development prospect and investment strategy of the global and Chinese laser chip industry Ⓑ 2022 ~ 2027
用命令行 给 apk 签名
MySQL gap lock
Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices