当前位置:网站首页>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
边栏推荐
猜你喜欢

正大留4的主账户信息汇总

06 装饰(Decorator)模式

Johnson–Lindenstrauss Lemma(2)

Super detailed pycharm tutorial

2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路

Vmware安装win10报错:operating system not found
![Introduction to Luogu 3 [circular structure] problem list solution](/img/fd/c0c5687c7e6e74bd5c911b27c3e19c.png)
Introduction to Luogu 3 [circular structure] problem list solution

idea自動導包和自動删包設置

C# 基于MQTTNet的服务端与客户端通信案例

Lay the foundation for children's programming to become a basic discipline
随机推荐
数据库问题汇总
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
VMware installation win10 reports an error: operating system not found
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
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
解决:代理抛出异常错误
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
CubeMx DMA笔记
06 装饰(Decorator)模式
Basic differences between Oracle and MySQL (entry level)
Ruby replaces gem Alibaba image
【ClickHouse】How to create index for Map Type Column or one key of it?
Case sharing | intelligent Western Airport
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Oracle stored procedure and job task setting
DJB Hash
案例分享|智慧化的西部机场
6.30年终小结,学生时代结束
Getting started with pytest ----- confitest Application of PY
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing