当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
Greenplum 6.x common statements
Download and install orcale database11.2.0.4
Novice entry SCM must understand those things
Introduction to data fragmentation
[hard core science popularization] working principle of dynamic loop monitoring system
为什么要选择云原生数据库
LeetCode 715. Range 模块
Componentspace2022, assertions, protocols, bindings, and configuration files
关于基于kangle和EP面板使用CDN
【MySQL】数据库进阶之触发器内容详解
随机推荐
Count sort (diagram)
Quick sorting (detailed illustration of single way, double way, three way)
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
Virtual address space
JS operation
GOLand idea intellij 无法输入汉字
ES6_ Arrow function
Newly found yii2 excel processing plug-in
Greenplum 6.x monitoring software setup
AVL balanced binary search tree
String operation
Qt Charts使用(重写QChartView,实现一些自定义功能)
[MySQL] detailed explanation of trigger content of database advanced
[Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
使用AGC重签名服务前后渠道号信息异常分析
Interpolation lookup (two methods)
Laravel8 uses passport login and JWT (generate token)
Download and install orcale database11.2.0.4
NCS Chengdu Xindian interview experience
Introduction to data fragmentation