当前位置:网站首页>LeetCode 736. LISP syntax parsing
LeetCode 736. LISP syntax parsing
2022-07-07 08:50:00 【Sasakihaise_】
【DFS】 String parsing + recursive
First extract the operator , Later, it will be split according to the concept of sub expression , Put it in a list in .
If the operator is let, So two by two traversal , The first string is used as key, The second string is an expression , Continue recursive parsing , Take the return value as value Deposit together map in . Then return the parsed value of the last string .
If the operator is add, Then you only need to add the parsing results of the two expressions to return .
The operator is mult Empathy .
class Solution {
// dfs 9:32 11:00
int ans = 0;
int dfs(Map<String, Integer> map, String ex) {
if ((ex.charAt(0) >= '0' && ex.charAt(0) <= '9') || ex.charAt(0) == '-') {
return Integer.parseInt(ex);
}
if (ex.charAt(0) == '(') {
ex = ex.substring(1, ex.length() - 1);
}
// System.out.println(ex);
// for (var k: map.keySet()) {
// System.out.print(k + "," + map.get(k) + ";");
// }
// System.out.println();
int n = ex.length(), i = 0, j = 0;
while (j < n && ex.charAt(j) != ' ') {
j++;
}
List<String> list = new ArrayList();
String op = ex.substring(i, j);
i = ++j;
int t = 0;
char c;
if (op.equals("let")) {
while (i < n) {
while (j < n) {
c = ex.charAt(j);
if (t != 0) {
if (c == ')') {
t--;
} else if (c == '(') {
t++;
}
} else {
if (c == '(') {
t++;
} else if (c == ' ') {
break;
}
}
j++;
}
list.add(ex.substring(i, j));
i = ++j;
}
int m = list.size() - 1;
Map<String, Integer> next = new HashMap(map);
for (i = 0; i < m; i += 2) {
char ch = list.get(i + 1).charAt(0);
if ((ch >= '0' && ch <= '9') || ch == '-') {
next.put(list.get(i), Integer.parseInt(list.get(i + 1)));
} else {
next.put(list.get(i), dfs(next, list.get(i + 1)));
}
}
ans = dfs(next, list.get(m));
return ans;
} else if (op.equals("add") || op.equals("mult")) {
while (i < n) {
while (j < n) {
c = ex.charAt(j);
if (t != 0) {
if (c == ')') {
t--;
} else if(c == '(') {
t++;
}
} else {
if (c == '(') {
t++;
} else if (c == ' ') {
break;
}
}
j++;
}
list.add(ex.substring(i, j));
i = ++j;
}
int ret = 0;
if (op.equals("add")) {
for (var e: list) {
if (map.containsKey(e)) {
ret += map.get(e);
} else {
ret += dfs(map, e);
}
}
} else {
ret = 1;
for (var e: list) {
if (map.containsKey(e)) {
ret *= map.get(e);
} else {
ret *= dfs(map, e);
}
}
}
return ret;
} else {
// System.out.println(op);
return map.get(op);
}
}
public int evaluate(String expression) {
Map<String, Integer> map = new HashMap();
return dfs(map, expression);
// return ans;
}
}
边栏推荐
- [Yugong series] February 2022 U3D full stack class 008 - build a galaxy scene
- let const
- How to realize the high temperature alarm of the machine room in the moving ring monitoring system
- 数字三角形模型 AcWing 1027. 方格取数
- [wechat applet: cache operation]
- opencv之图像分割
- [Yugong series] February 2022 U3D full stack class 006 unity toolbar
- Greenplum6.x搭建_环境配置
- 9c09730c0eea36d495c3ff6efe3708d8
- 阿里p8手把手教你,自动化测试应该如何实现多线程?赶紧码住
猜你喜欢
[Yugong series] February 2022 U3D full stack class 006 unity toolbar
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
let const
Pointer advanced, string function
Virtual address space
Exercise arrangement 2.10, 11
快速集成认证服务-HarmonyOS平台
Other 7 features of TCP [sliding window mechanism ▲]
Greenplum6.x常用语句
How to realize sliding operation component in fast application
随机推荐
Composer change domestic image
Other 7 features of TCP [sliding window mechanism ▲]
测试人一定要会的技能:selenium的三种等待方式解读,清晰明了
Enterprise manager cannot connect to the database instance
[Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
Redis summary
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
How to realize sliding operation component in fast application
Frequently Asked Coding Problems
Gson converts the entity class to JSON times declare multiple JSON fields named
Rapid integration of authentication services - harmonyos platform
Golan idea IntelliJ cannot input Chinese characters
A bug using module project in idea
GOLand idea intellij 无法输入汉字
对API接口或H5接口做签名认证
Novice entry SCM must understand those things
Mock. JS usage details
Why choose cloud native database
更改当前文件夹及文件夹下文件日期shell脚本
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design