当前位置:网站首页>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];
}
边栏推荐
- Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
- [daily question] the next day
- Page title component
- 2022 compilation principle final examination recall Edition
- Memory management of C
- 高级性能测试系列《24. 通过jdbc执行sql脚本》
- R language uses lrtest function of epidisplay package to perform likelihood ratio test on multiple GLM models (logisti regression). Compare whether the performance of the two models is different, and
- golang:[]byte转string
- [paper reading] Ca net: leveraging contextual features for lung cancer prediction
- Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
猜你喜欢
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept

High frequency interview questions

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5

拦截器与过滤器的区别

Kubernetes three open interfaces first sight

Processing strategy of message queue message loss and repeated message sending

Introduction to the paper | application of machine learning in database cardinality estimation

聊聊电商系统中红包活动设计

How to copy and paste interlaced in Excel

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
随机推荐
电商系统中常见的 9 大坑,你踩过没?
Learn the knowledge points of eight part essay ~ ~ 1
R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
学习八股文的知识点~~1
Excel如何进行隔行复制粘贴
R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
Kubernetes three open interfaces first sight
Markdown basic grammar
从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
[daily question] first day
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
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference
2022软件工程期末考试 回忆版
ORA-01455: converting column overflows integer datatype
开发固定资产管理系统,开发固定资产管理系统用什么语音
A4988驱动步进电机「建议收藏」
PHP-Parser羽毛球预约小程序开发require线上系统