当前位置:网站首页>LeetCode 241. Design priorities for operational expressions
LeetCode 241. Design priorities for operational expressions
2022-07-03 09:02:00 【Sasakihaise_】
241. Design priorities for operation expressions

【 Divide and conquer 】 Divide left and right according to operation symbols , After recursively processing the left and right sides, the possible results on the left and right sides are combined .
class Solution {
// Divide and conquer 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);
}
}【 Optimize 】 Optimize string processing , use StringBuilder Splicing
class Solution {
// Divide and conquer 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);
}
}
边栏推荐
- ES6 promise learning notes
- Log4j2 vulnerability recurrence and analysis
- Using DLV to analyze the high CPU consumption of golang process
- The method of replacing the newline character '\n' of a file with a space in the shell
- 传统企业数字化转型需要经过哪几个阶段?
- 20220630学习打卡
- Complex character + number pyramid
- LeetCode 438. 找到字符串中所有字母异位词
- [rust notes] 11 practical features
- LeetCode 324. 摆动排序 II
猜你喜欢
随机推荐
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
拯救剧荒,程序员最爱看的高分美剧TOP10
Discussion on enterprise informatization construction
Binary tree sorting (C language, int type)
传统企业数字化转型需要经过哪几个阶段?
Gif remove blank frame frame number adjustment
Log4j2 vulnerability recurrence and analysis
On a un nom en commun, maître XX.
The difference between if -n and -z in shell
20220630 learning clock in
TP5 order multi condition sort
Parameters of convolutional neural network
Common DOS commands
Binary tree sorting (C language, char type)
XPath实现XML文档的查询
Basic knowledge of network security
Common penetration test range
Es8 async and await learning notes
Complex character + number pyramid
注解简化配置与启动时加载









