当前位置:网站首页>[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
边栏推荐
- Analysis report on the development prospect and investment strategic planning of China's wafer manufacturing Ⓔ 2022 ~ 2028
- 关于佛萨奇2.0“Meta Force原力元宇宙系统开发逻辑方案(详情)
- 6. Wiper part
- Asp. NETCORE uses dynamic to simplify database access
- Analysis report on the development trend and Prospect of new ceramic materials in the world and China Ⓐ 2022 ~ 2027
- 波浪动画彩色五角星loader加载js特效
- Professor Li Zexiang, Hong Kong University of science and technology: I'm wrong. Why is engineering consciousness more important than the best university?
- Social distance (cow infection)
- 8 popular recommended style layout
- La taille de la pile spécifiée est petite, spécifiée à la sortie 328k
猜你喜欢

进入前六!博云在中国云管理软件市场销量排行持续上升

La taille de la pile spécifiée est petite, spécifiée à la sortie 328k
MySQL报错1040Too many connections的原因以及解决方案

研发效能度量框架解读

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?
基于mysql乐观锁实现秒杀的示例代码

详细讲解面试的 IO多路复用,select,poll,epoll

Jenkins+webhooks- multi branch parametric construction-

MySQL statistical bill information (Part 2): data import and query

Anti fraud, refusing to gamble, safe payment | there are many online investment scams, so it's impossible to make money like this
随机推荐
5. Use of ly tab plug-in of header component
IO的几种模型 阻塞,非阻塞,io多路复用,信号驱动和异步io
Anti fraud, refusing to gamble, safe payment | there are many online investment scams, so it's impossible to make money like this
La taille de la pile spécifiée est petite, spécifiée à la sortie 328k
[development of large e-commerce projects] performance pressure test - basic concept of pressure test & jmeter-38
Report on the "14th five year plan" and scale prospect prediction of China's laser processing equipment manufacturing industry Ⓢ 2022 ~ 2028
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
进入前六!博云在中国云管理软件市场销量排行持续上升
Social distance (cow infection)
Beidou communication module Beidou GPS module Beidou communication terminal DTU
Analysis report on the development prospect and investment strategic planning of China's wafer manufacturing Ⓔ 2022 ~ 2028
Introduction to topological sorting
ArrayList扩容机制以及线程安全性
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
6. Wiper part
受益互联网出海 汇量科技业绩重回高增长
Explain IO multiplexing, select, poll, epoll in detail
Computer network interview knowledge points
二传感器尺寸「建议收藏」
When you really learn databinding, you will find "this thing is really fragrant"!