当前位置:网站首页>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
边栏推荐
- js中的Map(含leetcode例题)
- Common errors of dmrman offline backup
- Splice characters in {{}}
- el-cascader回显只选中不显示的问题
- 面试会问的 Promise.all()
- Fasttext text text classification
- C case of communication between server and client based on mqttnet
- Getting started with pytest -- description of fixture parameters
- Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
- [Yu Yue education] autumn 2021 reference materials of Tongji University
猜你喜欢
![[high speed bus] Introduction to jesd204b](/img/78/1a0a3672e63058da6d98da95aa3cf2.jpg)
[high speed bus] Introduction to jesd204b

Promise all()

Win10 disk management compressed volume cannot be started

2022-003arts: recursive routine of binary tree

win10 磁盘管理 压缩卷 无法启动问题

Learn BeanShell before you dare to say you know JMeter

Rhcsa --- work on the fourth day

Getting started with pytest ----- confitest Application of PY
![[Yu Yue education] autumn 2021 reference materials of Tongji University](/img/50/5136359b89a5d047fe648637643ad0.jpg)
[Yu Yue education] autumn 2021 reference materials of Tongji University

How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
随机推荐
How to recover deleted data in disk
数据库问题汇总
Go Chan's underlying principles
leetcode两数相加go实现
Pyechats 1.19 generate a web version of Baidu map
[common error] the DDR type of FPGA device is selected incorrectly
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
Vmware安装win10报错:operating system not found
[bus interface] Axi interface
Pytest learning ----- pytest assertion of interface automation testing
解析少儿编程中的动手搭建教程
Exercise notes 13 (effective letter ectopic words)
[high speed bus] Introduction to jesd204b
Typescript function details
Fasttext text text classification
Pyechart1.19 national air quality exhibition
Preparation for writing SAP ui5 applications using typescript
10 minute quick start UI automation ----- puppeter
Oracle和MySQL的基本区别(入门级)
CubeMx DMA笔记