当前位置:网站首页>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
边栏推荐
- Promise all()
- Summary of database problems
- 培养中小学生对教育机器人的热爱之心
- Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
- List of common bugs in software testing
- Summary of main account information of zhengdaliu 4
- Learn BeanShell before you dare to say you know JMeter
- How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
- Future trend of automated testing ----- self healing technology
- 关于Steam 教育的知识整理
猜你喜欢

Virtual machine installation deepin system

Cannot activate CONDA virtual environment in vscode

Lm09 Fisher inverse transform inversion mesh strategy

Pyechats 1.19 generate a web version of Baidu map

The El cascader echo only selects the questions that are not displayed

洛谷入门3【循环结构】题单题解

LeetCode 1175. 质数排列(质数判断+组合数学)

解决:代理抛出异常错误

Knowledge arrangement about steam Education

Save the CDA from the disc to the computer
随机推荐
设置滚动条默认样式 谷歌浏览器
数学知识——快速幂的理解及例题
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
案例分享|智慧化的西部机场
[high speed bus] Introduction to jesd204b
Super detailed pycharm tutorial
Embedded-c language-8-character pointer array / large program implementation
MMAP zero copy knowledge point notes
Simple and practical accounting software, so that accounts can be checked
[bus interface] Axi interface
leetcode两数相加go实现
Go implements leetcode rotation array
Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
Ruby replaces gem Alibaba image
在{{}}中拼接字符
Summary of main account information of zhengdaliu 4
Cubemx DMA notes
Future trend of automated testing ----- self healing technology
Rhcsa --- work on the fourth day
Steam教育的实际问题解决能力