当前位置:网站首页>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
边栏推荐
- 【ClickHouse】How to create index for Map Type Column or one key of it?
- Go GC garbage collection notes (three color mark)
- Summary of common string processing functions in C language
- Online incremental migration of DM database
- Cultivate primary and secondary school students' love for educational robots
- 关于Steam 教育的知识整理
- geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
- Practical problem solving ability of steam Education
- Mysql重点难题(2)汇总
- Common errors of dmrman offline backup
猜你喜欢
How to modify data file path in DM database
Leetcode merge sort linked list
What data does the main account of Zhengda Meiou 4 pay attention to?
解析少儿编程中的动手搭建教程
UNET deployment based on deepstream
Virtual machine installation deepin system
Gin framework learning code
One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
CubeMx DMA笔记
Idea automatic package import and automatic package deletion settings
随机推荐
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
I sorted out some basic questions about opencv AI kit.
Vmware安装win10报错:operating system not found
Leetcode basic programming: array
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
6.30 year end summary, end of student age
DMA Porter
正大美欧4的主账户关注什么数据?
画波形图_数字IC
js面试收藏试题1
函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
Knowledge arrangement about steam Education
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
Let genuine SMS pressure measurement open source code
Win10 disk management compressed volume cannot be started
List of common bugs in software testing
Learn AI safety monitoring project from zero [attach detailed code]
Fasttext text text classification
Orthogonal test method and function diagram method for test case design
在{{}}中拼接字符