当前位置:网站首页>[241. Design priority for operation expression]
[241. Design priority for operation expression]
2022-07-01 13:34:00 【[email protected]】
source : Power button (LeetCode)
describe :
Give you a string of numbers and operators expression , Combine numbers and operators according to different priorities , Calculate and return the results of all possible combinations . You can In any order Return to the answer .
The generated test case meets its corresponding output value in line with 32 Bit integer range , The number of different results does not exceed 104 .
Example 1:
Input :expression = "2-1-1"
Output :[0,2]
explain :
((2-1)-1) = 0
(2-(1-1)) = 2
Example 2:
Input :expression = "2*3-4*5"
Output :[-34,-14,-10,-10,10]
explain :
(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
Tips :
1 <= expression.length <= 20- expression By numbers and operators
'+'、'-'and'*'form . - All integer values in the input expression are in the range [0, 99]
Method 1 : Memory search
Ideas and algorithms

Code :
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);
}
};
Execution time :0 ms, In all C++ Defeated in submission 100.00% Users of
Memory consumption :7 MB, In all C++ Defeated in submission 85.25% Users of
Complexity analysis
Time complexity :O(2n), among n by ops Size .
Spatial complexity :O(2n), among n by ops Size .
Method 2 : Dynamic programming
Ideas and algorithms

Code :
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];
}
};
Execution time :0 ms, In all C++ Defeated in submission 100.00% Users of
Memory consumption :6.5 MB, In all C++ Defeated in submission 92.05% Users of
Complexity analysis
Time complexity :O(2n), among n by ops Size .
Spatial complexity :O(2n), among n by ops Size .
author:LeetCode-Solution
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011319094945.html
边栏推荐
- 陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
- 启动solr报错The stack size specified is too small,Specify at least 328k
- Investment analysis and prospect prediction report of global and Chinese p-nitrotoluene industry Ⓙ 2022 ~ 2027
- [machine learning] VAE variational self encoder learning notes
- Summary of 20 practical typescript single line codes
- Beidou communication module Beidou GPS module Beidou communication terminal DTU
- 单工,半双工,全双工区别以及TDD和FDD区别
- Social distance (cow infection)
- 用命令行 给 apk 签名
- 流量管理技术
猜你喜欢

Svg diamond style code

Introduction to reverse debugging PE structure input table output table 05/07

当你真的学会DataBinding后,你会发现“这玩意真香”!

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

Summary of interview questions (1) HTTPS man in the middle attack, the principle of concurrenthashmap, serialVersionUID constant, redis single thread,

Google Earth Engine(GEE)——全球人类居住区网格数据 1975-1990-2000-2014 (P2016)

MySQL 66 questions, 20000 words + 50 pictures in detail! Necessary for review

【机器学习】VAE变分自编码器学习笔记

介绍一种对 SAP GUI 里的收藏夹事务码管理工具增强的实现方案

MySQL六十六问,两万字+五十图详解!复习必备
随机推荐
Listen in the network
Flow management technology
Wave animation color five pointed star loader loading JS special effects
LeetCode重建二叉树详解[通俗易懂]
基于mysql乐观锁实现秒杀的示例代码
Global and Chinese silicone defoamer production and marketing demand and investment forecast analysis report Ⓨ 2022 ~ 2027
Yarn重启applications记录恢复
单工,半双工,全双工区别以及TDD和FDD区别
简单的两个圆球loading加载
Introduction to reverse debugging PE structure input table output table 05/07
JS discolored Lego building blocks
Summary of interview questions (1) HTTPS man in the middle attack, the principle of concurrenthashmap, serialVersionUID constant, redis single thread,
China NdYAG crystal market research conclusion and development strategy proposal report Ⓥ 2022 ~ 2028
进入前六!博云在中国云管理软件市场销量排行持续上升
Some summary of pyqt5 learning (overview of the general meaning of some signals and methods)
Social distance (cow infection)
Word2vec training Chinese word vector
Detailed explanation of parallel replication examples in MySQL replication
1.8新特性-List
5. Use of ly tab plug-in of header component