当前位置:网站首页>LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
2022-07-02 05:01:00 【xylitolz】
0 题目


1 解题思路
1.1 分治 + 记忆化递归
将字符串按照运算符拆分成左右两个子字符串,分别计算出左右两个子字符串的结果集,再按照拆分运算符对结果集进行合并,对于子字符串也可以通过不断拆分来获得结果集(递归)
以 2 * 3 - 4 * 5 为例,可拆分为:
2和3 - 4 * 5两部分,中间是 * 号相连2 * 3和4 * 5两部分,中间是 - 号相连2 * 3 - 4和5两部分,中间是 * 号相连
为了防止重复计算,可以使用Map来保存子字符串对应的结果集。
1.1.1 代码实现
class Solution {
// 记忆化递归
Map<String, List<Integer>> mem = new HashMap<>();
public List<Integer> diffWaysToCompute(String expression) {
if (expression.length() == 0) {
return new ArrayList<>();
}
// 当前表达式已经有解了,直接返回
if (mem.containsKey(expression)) {
return mem.get(expression);
}
// 保存当前表达式的结果
List<Integer> res = new ArrayList<>();
int num = 0;
int index = 0;
// 考虑表达式仅仅只有一个数字的情况
while (index < expression.length() && !isOperation(expression.charAt(index))) {
num = num * 10 + (expression.charAt(index) - '0');
index++;
}
if (index == expression.length()) {
res.add(num);
mem.put(expression, res);
return res;
}
// 继续分割当前表达式 按照运算符
for (int i = 0; i < expression.length(); i++) {
if (isOperation(expression.charAt(i))) {
List<Integer> resLeft = diffWaysToCompute(expression.substring(0, i));
List<Integer> resRight = diffWaysToCompute(expression.substring(i + 1));
// 按运算符结合两个结果集
for (int j = 0; j < resLeft.size(); j++) {
for (int k = 0; k < resRight.size(); k++) {
char operation = expression.charAt(i);
res.add(calculate(resLeft.get(j), operation, resRight.get(k)));
}
}
}
}
// 保存到map
mem.put(expression, res);
return res;
}
private int calculate(int a, char op, int b) {
switch (op) {
case '+' :
return a + b;
case '-' :
return a - b;
case '*' :
return a * b;
}
return -1;
}
private boolean isOperation(char c) {
return !Character.isDigit(c);
}
}
1.2 动态规划
2 Reference
边栏推荐
- 数据库问题汇总
- Typescript function details
- Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
- [bus interface] Axi interface
- Getting started with pytest ----- confitest Application of PY
- Go GC garbage collection notes (three color mark)
- 农业生态领域智能机器人的应用
- CubeMx DMA笔记
- Unity particle Foundation
- Pyflink writes MySQL examples with JDBC
猜你喜欢

Embedded-c language-8-character pointer array / large program implementation

idea自动导包和自动删包设置

10 minute quick start UI automation ----- puppeter

Common errors of dmrman offline backup

Idea autoguide package and autodelete package Settings

Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products

Lm09 Fisher inverse transform inversion mesh strategy

Record my pytorch installation process and errors

06 decorator mode

将光盘中的cda保存到电脑中
随机推荐
Solution: the agent throws an exception error
Domestic all Chinese automatic test software apifox
案例分享|智慧化的西部机场
oracle 存储过程与job任务设置
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
Gin framework learning code
TypeScript类的使用
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
画波形图_数字IC
How to configure PostgreSQL 12.9 to allow remote connections
将光盘中的cda保存到电脑中
Leetcode basic programming: array
奠定少儿编程成为基础学科的原理
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Hcip day 17
Getting started with pytest ----- confitest Application of PY
LM09丨费雪逆变换反转网格策略
Oracle和MySQL的基本区别(入门级)
[high speed bus] Introduction to jesd204b
How to modify data file path in DM database