当前位置:网站首页>2022.7.1-----leetcode. two hundred and forty-one
2022.7.1-----leetcode. two hundred and forty-one
2022-07-02 19:19:00 【Lu 727】
// Three operators
static final int ADDITION = -1;
static final int SUBTRACTION = -2;
static final int MULTIPLICATION = -3;
public List<Integer> diffWaysToCompute(String expression) {
List<Integer> ops = new ArrayList<Integer>();
// Split expressions into numbers and operators
for (int i = 0; i < expression.length();) {
if (!Character.isDigit(expression.charAt(i))) {
if (expression.charAt(i) == '+') {
ops.add(ADDITION);
} else if (expression.charAt(i) == '-') {
ops.add(SUBTRACTION);
} else {
ops.add(MULTIPLICATION);
}
i++;
} else {
int t = 0;
while (i < expression.length() &&Character.isDigit(expression.charAt(i)))
{
t = t * 10 + expression.charAt(i) - '0';
i++;
}
ops.add(t);
}
}
// Record [l,r] Result of expression
List<Integer>[][] dp = new List[ops.size()][ops.size()];
for (int i = 0; i < ops.size(); i++) {
for (int j = 0; j < ops.size(); j++) {
dp[i][j] = new ArrayList<Integer>();
}
}
return dfs(dp, 0, ops.size() - 1, ops);
}
public List<Integer> dfs(List<Integer>[][] dp, int l, int r, List<Integer> ops) {
// Non empty indicates that the expression has been solved
if (dp[l][r].isEmpty()) {
if (l == r) {
dp[l][r].add(ops.get(l));// There's only one number
} else {
// By the end of i+1 Bit operator is split , Solve in the left and right sub formulas respectively
for (int i = l; i < r; i += 2) {
List<Integer> left = dfs(dp, l, i, ops);
List<Integer> right = dfs(dp, i + 2, r, ops);
// This operator is the last step , The left and right subformulas should combine all possible results in pairs
for (int lv : left) {
for (int rv : right) {
if (ops.get(i + 1) == ADDITION) {
dp[l][r].add(lv + rv);
} else if (ops.get(i + 1) == SUBTRACTION) {
dp[l][r].add(lv - rv);
} else {
dp[l][r].add(lv * rv);
}
}
}
}
}
}
return dp[l][r];
}
边栏推荐
- Excel finds the same value in a column, deletes the row or replaces it with a blank value
- 2022编译原理期末考试 回忆版
- GMapping代码解析[通俗易懂]
- Markdown basic grammar
- 以太网PHY层芯片LAN8720A简介
- 使用xml文件打印mybaties-log插件的方式
- 电商系统中常见的 9 大坑,你踩过没?
- The difference between interceptor and filter
- MySQL高级(进阶)SQL语句
- R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
猜你喜欢
MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
[100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
[fluent] dart data type (VaR data type | object data type)
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
使用CLion编译OGLPG-9th-Edition源码
How to copy and paste interlaced in Excel
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
随机推荐
metric_logger小解
Masa framework - DDD design (1)
Tips for material UV masking
性能测试如何创造业务价值
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
ICDE 2023|TKDE Poster Session(CFP)
Page title component
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
How to delete the border of links in IE? [repeat] - how to remove borders around links in IE? [duplicate]
2022 software engineering final exam recall Edition
线程应用实例
QT中的QPropertyAnimation使用和toast案列
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
Processing strategy of message queue message loss and repeated message sending
R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
《病人家属,请来一下》读书笔记
以太网PHY层芯片LAN8720A简介
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
Crypto usage in nodejs