当前位置:网站首页>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 :
2
and3 - 4 * 5
Two parts , In the middle is * No. connected2 * 3
and4 * 5
Two parts , In the middle is - No. connected2 * 3 - 4
and5
Two 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
边栏推荐
- 解决:代理抛出异常错误
- A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
- Analyzing the hands-on building tutorial in children's programming
- 从数组中找出和为目标的下标
- VMware installation win10 reports an error: operating system not found
- MMAP zero copy knowledge point notes
- 案例分享|智慧化的西部机场
- Gin framework learning code
- Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
- 培养中小学生对教育机器人的热爱之心
猜你喜欢
Solution: the agent throws an exception error
Record my pytorch installation process and errors
Unity particle Foundation
Collectors.groupingBy 排序
Promise all()
Detailed process of DC-1 range construction and penetration practice (DC range Series)
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
数据库问题汇总
List of common bugs in software testing
[bus interface] Axi interface
随机推荐
Embedded-c language-9-makefile/ structure / Consortium
Map in JS (including leetcode examples)
Beginner crawler - biqu Pavilion crawler
Getting started with pytest ----- confitest Application of PY
VMware installation win10 reports an error: operating system not found
LeetCode 1175. 质数排列(质数判断+组合数学)
Change deepin to Alibaba image source
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
The El cascader echo only selects the questions that are not displayed
从数组中找出和为目标的下标
面试会问的 Promise.all()
leetcode两数相加go实现
Online incremental migration of DM database
Case sharing | intelligent Western Airport
Idea autoguide package and autodelete package Settings
Summary of MySQL key challenges (2)
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
How to configure PostgreSQL 12.9 to allow remote connections
6.30 year end summary, end of student age
Cultivate primary and secondary school students' love for educational robots