当前位置:网站首页>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);
}
}
边栏推荐
- [concurrent programming] consistency hash
- Character pyramid
- [concurrent programming] atomic operation CAS
- Es8 async and await learning notes
- [rust notes] 13 iterator (Part 1)
- DOM 渲染系统(render mount patch)响应式系统
- [RPC] RPC remote procedure call
- First Servlet
- Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
- How does unity fixedupdate call at a fixed frame rate
猜你喜欢

状态压缩DP AcWing 91. 最短Hamilton路径

【Rust笔记】02-所有权
![[RPC] RPC remote procedure call](/img/dc/872204ea47fcff04cdb72e18a2a4ef.jpg)
[RPC] RPC remote procedure call

UE4 source code reading_ Mobile synchronization

PHP uses foreach to get a value in a two-dimensional associative array (with instances)

DOM render mount patch responsive system
![[redis] redis persistent RDB vs AOF (source code)](/img/57/b6a86c49cedee31fc00dc5d1372023.jpg)
[redis] redis persistent RDB vs AOF (source code)

Try to reprint an article about CSDN reprint

第一个Servlet

Binary tree sorting (C language, int type)
随机推荐
Location of package cache downloaded by unity packagemanager
Unity editor expansion - controls, layouts
How does unity fixedupdate call at a fixed frame rate
Message queue for interprocess communication
Annotations simplify configuration and loading at startup
createjs easeljs
Mortgage Calculator
[rust notes] 09- special types and generics
【Rust 笔记】10-操作符重载
[concurrent programming] concurrent tool class of thread
Monotonic stack -84 The largest rectangle in the histogram
【Rust 笔记】08-枚举与模式
JS ternary operator - learning notes (with cases)
Parameters of convolutional neural network
[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
数位统计DP AcWing 338. 计数问题
MySQL three logs
[RPC] RPC remote procedure call
Try to reprint an article about CSDN reprint
TP5 order multi condition sort