当前位置:网站首页>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);
}
}
边栏推荐
- LeetCode 715. Range 模块
- 教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
- 干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
- Wonderful review | i/o extended 2022 activity dry goods sharing
- 剑指 Offer II 029. 排序的循环链表
- Life cycle of Servlet
- Alibaba canal actual combat
- Gaussian elimination acwing 883 Gauss elimination for solving linear equations
- Deeply understand the underlying data structure of MySQL index
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
猜你喜欢
[concurrent programming] explicit lock and AQS
Notes and bugs generated during the use of h:i:s and y-m-d
Sending and receiving of request parameters
Phpstudy 80 port occupied W10 system
Monotonic stack -503 Next bigger Element II
剑指 Offer II 029. 排序的循环链表
Monotonic stack -42 Connect rainwater
第一个Servlet
记忆化搜索 AcWing 901. 滑雪
Concurrent programming (III) detailed explanation of synchronized keyword
随机推荐
JS ternary operator - learning notes (with cases)
求组合数 AcWing 885. 求组合数 I
传统企业数字化转型需要经过哪几个阶段?
TP5 multi condition sorting
Binary tree sorting (C language, char type)
The method of replacing the newline character '\n' of a file with a space in the shell
精彩回顾|I/O Extended 2022 活动干货分享
Get the link behind? Parameter value after question mark
求组合数 AcWing 886. 求组合数 II
Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
LeetCode 871. 最低加油次数
C language student management system based on linked list, super detailed
Really explain the five data structures of redis
推荐一个 yyds 的低代码开源项目
LeetCode 30. 串联所有单词的子串
Dom4j遍历和更新XML
Gif remove blank frame frame number adjustment
Apache startup failed phpstudy Apache startup failed
网络安全必会的基础知识
Allocation exception Servlet