当前位置:网站首页>[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
边栏推荐
- 【机器学习】VAE变分自编码器学习笔记
- MySQL Replication中的并行复制示例详解
- Analysis report on the development prospect and investment strategy of the global and Chinese laser chip industry Ⓑ 2022 ~ 2027
- Detailed explanation of parallel replication examples in MySQL replication
- leetcode 322. Coin change (medium)
- Svg diamond style code
- SAP 智能机器人流程自动化(iRPA)解决方案分享
- minimum spanning tree
- 内容审计技术
- 5G工业网关的科技治超应用 超限超重超速非现场联合执法
猜你喜欢

Spark source code (V) how does dagscheduler taskscheduler cooperate with submitting tasks, and what is the corresponding relationship between application, job, stage, taskset, and task?

JS discolored Lego building blocks

Content Audit Technology

Judea pearl, Turing prize winner: 19 causal inference papers worth reading recently

北斗通信模块 北斗gps模块 北斗通信终端DTU

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

研发效能度量框架解读
Example code of second kill based on MySQL optimistic lock

Wave animation color five pointed star loader loading JS special effects
Reasons for MySQL reporting 1040too many connections and Solutions
随机推荐
Svg diamond style code
3.4 《数据库系统概论》之数据查询—SELECT(单表查询、连接查询、嵌套查询、集合查询、多表查询)
Detailed explanation of leetcode reconstruction binary tree [easy to understand]
MySQL Replication中的并行复制示例详解
启动solr报错The stack size specified is too small,Specify at least 328k
Summary of interview questions (1) HTTPS man in the middle attack, the principle of concurrenthashmap, serialVersionUID constant, redis single thread,
陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
单工,半双工,全双工区别以及TDD和FDD区别
spark源码阅读总纲
SAP 智能机器人流程自动化(iRPA)解决方案分享
The stack size specified is too small, specify at least 328k
20个实用的 TypeScript 单行代码汇总
新手准备多少钱可以玩期货?农产品可以吗?
机器学习—性能度量
In the next stage of digital transformation, digital twin manufacturer Youyi technology announced that it had completed a financing of more than 300 million yuan
开源者的自我修养|为 ShardingSphere 贡献了千万行代码的程序员,后来当了 CEO
MySQL六十六问,两万字+五十图详解!复习必备
【机器学习】VAE变分自编码器学习笔记
3.4 data query in introduction to database system - select (single table query, connection query, nested query, set query, multi table query)
SVG钻石样式代码