当前位置:网站首页>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];
}
边栏推荐
- ORA-01455: converting column overflows integer datatype
- 电脑使用哪个录制视频软件比较好
- Emmet basic syntax
- When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
- Progress progress bar
- golang:[]byte转string
- Yunna | why use the fixed asset management system and how to enable it
- Progress-进度条
- A4988驱动步进电机「建议收藏」
- 线程应用实例
猜你喜欢

使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星

ICDE 2023|TKDE Poster Session(CFP)

M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)

Have you stepped on the nine common pits in the e-commerce system?

Markdown基础语法

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5

Machine learning notes - time series prediction research: monthly sales of French champagne
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept
![[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction](/img/ef/bb48ee88d5dc6fe876a498ab53106e.png)
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction

Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
随机推荐
以太网PHY层芯片LAN8720A简介
FastDFS安装
2022 compilation principle final examination recall Edition
Develop fixed asset management system, what voice is used to develop fixed asset management system
新手必看,點擊兩個按鈕切換至不同的內容
Machine learning notes - time series prediction research: monthly sales of French champagne
A4988驱动步进电机「建议收藏」
yolov3 训练自己的数据集之生成train.txt
2022 software engineering final exam recall Edition
PyTorch函数中的__call__和forward函数
R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
[daily question] first day
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
Use cheat engine to modify money, life and stars in Kingdom rush
How to delete the border of links in IE? [repeat] - how to remove borders around links in IE? [duplicate]
R语言dplyr包filter函数筛选dataframe数据、如果需要筛选的数据列(变量)名称中包含引号则需要使用!!sym语法处理、否则因为无法处理引号筛选不到任何数据
R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径