当前位置:网站首页>LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
2022-07-02 05:04:00 【xylitolz】
List of articles
0 subject


1 Their thinking
1.1 Divide and conquer + Memory recursion
String by Operator Split into left and right substrings , Calculate the result set of the left and right substrings respectively , Then merge the result set according to the split operator , For substrings, the result set can also be obtained by continuous splitting ( recursive )
With 2 * 3 - 4 * 5 For example , Split into :
2and3 - 4 * 5Two parts , In the middle is * No. connected2 * 3and4 * 5Two parts , In the middle is - No. connected2 * 3 - 4and5Two parts , In the middle is * No. connected
To prevent double counting , have access to Map To save the result set corresponding to the substring .
1.1.1 Code implementation
class Solution {
// Memory recursion
Map<String, List<Integer>> mem = new HashMap<>();
public List<Integer> diffWaysToCompute(String expression) {
if (expression.length() == 0) {
return new ArrayList<>();
}
// The current expression has been solved , Go straight back to
if (mem.containsKey(expression)) {
return mem.get(expression);
}
// Save the result of the current expression
List<Integer> res = new ArrayList<>();
int num = 0;
int index = 0;
// Consider the case where the expression has only one number
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;
}
// Continue splitting the current expression By operator
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));
// Combine two result sets by operator
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)));
}
}
}
}
// Save to 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 Dynamic programming
2 Reference
边栏推荐
- 【ClickHouse】How to create index for Map Type Column or one key of it?
- [Yu Yue education] autumn 2021 reference materials of Tongji University
- What data does the main account of Zhengda Meiou 4 pay attention to?
- Rhcsa --- work on the fourth day
- LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
- Application d'un robot intelligent dans le domaine de l'agroécologie
- How to modify data file path in DM database
- Virtual machine installation deepin system
- DMA Porter
- Idea autoguide package and autodelete package Settings
猜你喜欢

Summary of database problems

Lay the foundation for children's programming to become a basic discipline

Virtual machine installation deepin system

Getting started with pytest -- description of fixture parameters

TypeScript函数详解

Embedded-c language-9-makefile/ structure / Consortium

How to write a client-side technical solution

将光盘中的cda保存到电脑中

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

Here comes the chicken soup! Keep this quick guide for data analysts
随机推荐
TypeScript函数详解
Here comes the chicken soup! Keep this quick guide for data analysts
Summary of main account information of zhengdaliu 4
Feign realizes file uploading and downloading
農業生態領域智能機器人的應用
在{{}}中拼接字符
Win10 disk management compressed volume cannot be started
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
Pytest learning ----- pytest assertion of interface automation testing
Mapping settings in elk (8) es
正大美欧4的主账户关注什么数据?
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
Embedded-c language-9-makefile/ structure / Consortium
Summary of database problems
10 minute quick start UI automation ----- puppeter
[high speed bus] Introduction to jesd204b
Map in JS (including leetcode examples)