当前位置:网站首页>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);
}
}
边栏推荐
- Notes on understanding applets 2022/7/3
- Unity editor expansion - the design idea of imgui
- too many open files解决方案
- [concurrent programming] Table hopping and blocking queue
- 20220630学习打卡
- Binary tree sorting (C language, char type)
- Life cycle of Servlet
- createjs easeljs
- Message pack in C deserializes array objects
- Dom4j遍历和更新XML
猜你喜欢
UE4 source code reading_ Bone model and animation system_ Animation compression
Unity editor expansion - draw lines
MySQL three logs
UE4 source code reading_ Bone model and animation system_ Animation node
Parameters of convolutional neural network
Graphics_ Games101/202 learning notes
OpenGL learning notes
Gif remove blank frame frame number adjustment
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
单调栈-42. 接雨水
随机推荐
Graphics_ Learnopongl learning notes
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
【Rust笔记】06-包和模块
Query XML documents with XPath
【Rust 笔记】08-枚举与模式
Alibaba canaladmin deployment and canal cluster Ha Construction
Format - C language project sub file
[rust notes] 12 closure
【Rust笔记】05-错误处理
Phpstudy 80 port occupied W10 system
Dom4j traverses and updates XML
[rust notes] 05 error handling
[concurrent programming] Table hopping and blocking queue
Unity editor expansion - the framework and context of unity imgui
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
22-05-26 西安 面试题(01)准备
Servlet的生命周期
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
注解简化配置与启动时加载
Try to reprint an article about CSDN reprint