当前位置:网站首页>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;
}
}
边栏推荐
- Leetcode 1984. Minimum difference in student scores
- Greenplum 6.x common statements
- Arm GIC (IV) GIC V3 register class analysis notes.
- 说一个软件创业项目,有谁愿意投资的吗?
- LeetCode 736. Lisp 语法解析
- [Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
- Mountaineering team (DFS)
- What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
- MAC OSX php dyld: Library not loaded: /usr/local/xxxx. dylib
- Count sort (diagram)
猜你喜欢
快速集成认证服务-HarmonyOS平台
A bug using module project in idea
Novice entry SCM must understand those things
Greenplum 6.x build_ Environment configuration
Greenplum6.x-版本变化记录-常用手册
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
JS的操作
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
平台化,强链补链的一个支点
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
随机推荐
【MySQL】数据库进阶之触发器内容详解
All about PDF crack, a complete solution to meet all your PDF needs
Markdown编辑器Editor.md插件的使用
Exercise arrangement 2.10, 11
Shell script for changing the current folder and the file date under the folder
Problems encountered in the use of go micro
Teach you how to select PCB board by hand (II)
How to realize sliding operation component in fast application
ncs成都新電面試經驗
Mountaineering team (DFS)
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
Implementation of navigation bar at the bottom of applet
Qt Charts使用(重写QChartView,实现一些自定义功能)
Tips for using jeditabletable
let const
About using CDN based on Kangle and EP panel
Greenplum 6.x monitoring software setup
selenium自动化集成,八年测试经验软测工程师,一篇文章带你学懂
为什么要选择云原生数据库
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼