当前位置:网站首页>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
边栏推荐
- idea自动导包和自动删包设置
- Summary of MySQL key challenges (2)
- go实现leetcode旋转数组
- Go Chan's underlying principles
- Solution of DM database unable to open graphical interface
- Federal learning: dividing non IID samples according to Dirichlet distribution
- Mysql重点难题(2)汇总
- [high speed bus] Introduction to jesd204b
- How to recover deleted data in disk
- Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
猜你喜欢

Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market

Learn BeanShell before you dare to say you know JMeter

Orthogonal test method and function diagram method for test case design

2022-003arts: recursive routine of binary tree

Analyzing the hands-on building tutorial in children's programming

Application d'un robot intelligent dans le domaine de l'agroécologie

Cultivate primary and secondary school students' love for educational robots

Realize the function of data uploading

Video cover image setting, put cover images into multiple videos in the simplest way

06 decorator mode
随机推荐
oracle 存储过程与job任务设置
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
Oracle和MySQL的基本区别(入门级)
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
Cultivate primary and secondary school students' love for educational robots
Learn AI safety monitoring project from zero [attach detailed code]
数学知识(欧拉函数)
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
将光盘中的cda保存到电脑中
Markdown edit syntax
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Cannot activate CONDA virtual environment in vscode
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
[bus interface] Axi interface
Several methods of capturing packets under CS framework
Lm09 Fisher inverse transform inversion mesh strategy
C# 图片显示占用问题
Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
Orthogonal test method and function diagram method for test case design
Preparation for writing SAP ui5 applications using typescript