当前位置:网站首页>LeetCode 241. 为运算表达式设计优先级
LeetCode 241. 为运算表达式设计优先级
2022-07-03 08:49:00 【Sasakihaise_】

【分治】按照运算符号划分左右,递归处理左右后得到左右两侧可能的结果进行组合。
class Solution {
// 分治 9:30 10:34
List<String> list = new ArrayList();
List<Integer> diff(int start, int end) {
if (start == end) {
String str = list.get(start);
return new ArrayList<Integer>() {
{ add(Integer.parseInt(str)); }};
}
List<Integer> ans = new ArrayList();
for (var i = start; i <= end; i++) {
String str = list.get(i);
if (str.equals("+") || str.equals("-") || str.equals("*")) {
List<Integer> left = diff(start, i - 1);
List<Integer> right = diff(i + 1, end);
for (var x: left) {
for (var y: right) {
if (str.equals("+")) {
ans.add(x + y);
} else if(str.equals("-")) {
ans.add(x - y);
} else if (str.equals("*")) {
ans.add(x * y);
}
}
}
}
}
return ans;
}
public List<Integer> diffWaysToCompute(String expression) {
String num = "";
for (var i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (c >= '0' && c <= '9') {
num += c;
} else {
list.add(num);
num = "";
list.add("" + c);
}
}
list.add(num);
return diff(0, list.size() - 1);
}
}【优化】优化字符串处理,用StringBuilder进行拼接
class Solution {
// 分治 9:30 10:34
List<String> list = new ArrayList();
Map<Integer, List<Integer>> map = new HashMap();
List<Integer> diff(int start, int end) {
int key = start * 100 + end;
if (map.containsKey(key)) {
// System.out.println("en");
return map.get(key);
}
if (start == end) {
String str = list.get(start);
List<Integer> ret = new ArrayList();
ret.add(Integer.parseInt(str));
map.put(key, ret);
return ret;
}
List<Integer> ans = new ArrayList();
for (var i = start; i <= end; i++) {
String str = list.get(i);
if (str.equals("+") || str.equals("-") || str.equals("*")) {
List<Integer> left = diff(start, i - 1);
List<Integer> right = diff(i + 1, end);
for (var x: left) {
for (var y: right) {
if (str.equals("+")) {
ans.add(x + y);
} else if(str.equals("-")) {
ans.add(x - y);
} else if (str.equals("*")) {
ans.add(x * y);
}
}
}
}
}
map.put(key, ans);
return ans;
}
public List<Integer> diffWaysToCompute(String expression) {
StringBuilder sb = new StringBuilder();
for (var i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (c >= '0' && c <= '9') {
sb.append(c);
} else {
list.add(sb.toString());
sb = new StringBuilder();
list.add(String.valueOf(c));
}
}
list.add(sb.toString());
return diff(0, list.size() - 1);
}
}
边栏推荐
- Final review of Database Principles
- 数据库原理期末复习
- OpenGL learning notes
- Markdown learning
- Unity multi open script
- 【Rust 笔记】07-结构体
- Alibaba canaladmin deployment and canal cluster Ha Construction
- C language student management system based on linked list, super detailed
- Unity editor expansion - draw lines
- Drawing maze EasyX library with recursive backtracking method
猜你喜欢

Query XML documents with XPath

20220630学习打卡
![[concurrent programming] Table hopping and blocking queue](/img/b7/023991a00956e469af855e7a81e126.jpg)
[concurrent programming] Table hopping and blocking queue

记忆化搜索 AcWing 901. 滑雪

樹形DP AcWing 285. 沒有上司的舞會

Graphics_ Games101/202 learning notes

Deeply understand the underlying data structure of MySQL index

注解简化配置与启动时加载

MySQL three logs

Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
随机推荐
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
DOM render mount patch responsive system
【Rust 笔记】11-实用特型
cres
樹形DP AcWing 285. 沒有上司的舞會
On the difference and connection between find and select in TP5 framework
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
Unity Editor Extension - drag and drop
Message pack in C deserializes array objects
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
[rust notes] 05 error handling
Facial expression recognition based on pytorch convolution -- graduation project
Parameters of convolutional neural network
二进制转十进制,十进制转二进制
Redis cluster series 4
DOM 渲染系统(render mount patch)响应式系统
Binary tree sorting (C language, char type)
Binary tree sorting (C language, int type)
Markdown learning