当前位置:网站首页>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函数详解
- The El cascader echo only selects the questions that are not displayed
- Leetcode merge sort linked list
- Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
- 数学问题(数论)试除法做质数的判断、分解质因数,筛质数
- I sorted out some basic questions about opencv AI kit.
- Several methods of capturing packets under CS framework
- How to recover deleted data in disk
- Cultivate primary and secondary school students' love for educational robots
- Basic differences between Oracle and MySQL (entry level)
猜你喜欢
Gin framework learning code
Application d'un robot intelligent dans le domaine de l'agroécologie
Hcip day 17
Video cover image setting, put cover images into multiple videos in the simplest way
[bus interface] Axi interface
How to configure PostgreSQL 12.9 to allow remote connections
Mapping location after kotlin confusion
Learn AI safety monitoring project from zero [attach detailed code]
数学知识(欧拉函数)
How to recover deleted data in disk
随机推荐
Mathematical knowledge -- understanding and examples of fast power
The El cascader echo only selects the questions that are not displayed
培养中小学生对教育机器人的热爱之心
Embedded-c language-8-character pointer array / large program implementation
C # picture display occupancy problem
数学知识——快速幂的理解及例题
Detailed process of DC-1 range construction and penetration practice (DC range Series)
函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
[opencv] image binarization
Ruby replaces gem Alibaba image
Introduction to Luogu 3 [circular structure] problem list solution
UNET deployment based on deepstream
奠定少儿编程成为基础学科的原理
TypeScript函数详解
初学爬虫-笔趣阁爬虫
Basic differences between Oracle and MySQL (entry level)
06 装饰(Decorator)模式
解决:代理抛出异常错误
geotrust ov多域名ssl证书一年两千一百元包含几个域名?
[understand one article] FD_ Use of set