当前位置:网站首页>【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
边栏推荐
- Function test process in software testing
- MySQL Replication中的并行复制示例详解
- MySQL gap lock
- Google Earth engine (GEE) - Global Human Settlements grid data 1975-1990-2000-2014 (p2016)
- 不同的测试技术区分
- 单工,半双工,全双工区别以及TDD和FDD区别
- Listen in the network
- 【大型电商项目开发】性能压测-压力测试基本概念&JMeter-38
- 受益互联网出海 汇量科技业绩重回高增长
- 新手准备多少钱可以玩期货?农产品可以吗?
猜你喜欢
![[Niu Ke's questions -sql big factory interview real questions] no2 User growth scenario (a certain degree of information flow)](/img/a0/e9e7506c9c34986dc73562539c8410.png)
[Niu Ke's questions -sql big factory interview real questions] no2 User growth scenario (a certain degree of information flow)

启动solr报错The stack size specified is too small,Specify at least 328k

图灵奖得主Judea Pearl:最近值得一读的19篇因果推断论文

刘对(火线安全)-多云环境的风险发现
![[machine learning] VAE variational self encoder learning notes](/img/38/3eb8d9078b2dcbe780430abb15edcb.png)
[machine learning] VAE variational self encoder learning notes

French Data Protection Agency: using Google Analytics or violating gdpr

SAP intelligent robot process automation (IRPA) solution sharing

04 redis source code data structure dictionary

Jenkins+webhooks- multi branch parametric construction-

Svg diamond style code
随机推荐
ZABBIX 6.0 source code installation and ha configuration
The 14th five year plan of China's environmental protection industry and the report on the long-term goals for 2035 Ⓖ 2022 ~ 2028
Computer network interview knowledge points
游戏公会在去中心化游戏中的未来
What is the future development direction of people with ordinary education, appearance and family background? The career planning after 00 has been made clear
Colorful five pointed star SVG dynamic web page background JS special effect
启动solr报错The stack size specified is too small,Specify at least 328k
Report on the "14th five year plan" and scale prospect prediction of China's laser processing equipment manufacturing industry Ⓢ 2022 ~ 2028
基于mysql乐观锁实现秒杀的示例代码
Terminal identification technology and management technology
Application of 5g industrial gateway in scientific and technological overload control; off-site joint law enforcement for over limit, overweight and overspeed
运行游戏时出现0xc000007b错误的解决方法[通俗易懂]
Analysis report on the development pattern of China's smart emergency industry and the 14th five year plan Ⓠ 2022 ~ 2028
Analysis report on the development prospect and investment strategy of the global and Chinese laser chip industry Ⓑ 2022 ~ 2027
香港科技大学李泽湘教授:我错了,为什么工程意识比上最好的大学都重要?
minimum spanning tree
关于佛萨奇2.0“Meta Force原力元宇宙系统开发逻辑方案(详情)
Who should I know when opening a stock account? Is it actually safe to open an account online?
20个实用的 TypeScript 单行代码汇总
China NdYAG crystal market research conclusion and development strategy proposal report Ⓥ 2022 ~ 2028