当前位置:网站首页>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;
}
}
边栏推荐
- JEditableTable的使用技巧
- 最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
- 快速集成认证服务-HarmonyOS平台
- [Yugong series] February 2022 U3D full stack class 008 - build a galaxy scene
- Shell script for changing the current folder and the file date under the folder
- Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
- 登山小分队(dfs)
- [Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
- Mountaineering team (DFS)
- 关于基于kangle和EP面板使用CDN
猜你喜欢
![[MySQL] detailed explanation of trigger content of database advanced](/img/6c/8aad649e4ba1160db3aea857ecf4a1.png)
[MySQL] detailed explanation of trigger content of database advanced

21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
![[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources](/img/e3/3703bdace2d0ca47c1a585562dc15e.jpg)
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources

ncs成都新電面試經驗

快速集成认证服务-HarmonyOS平台

为什么要选择云原生数据库

How to realize sliding operation component in fast application

Laravel8 uses passport login and JWT (generate token)
![[南京大学]-[软件分析]课程学习笔记(一)-introduction](/img/57/bf652b36389d2bf95388d2eb4772a1.png)
[南京大学]-[软件分析]课程学习笔记(一)-introduction

A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
随机推荐
Compilation and linking of programs
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
LeetCode 736. Lisp 语法解析
年薪50w阿裏P8親自下場,教你如何從測試進階
You should use Google related products with caution
What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
[kuangbin] topic 15 digit DP
Greenplum 6.x version change record common manual
Newly found yii2 excel processing plug-in
Shell script for changing the current folder and the file date under the folder
[Yugong series] February 2022 U3D full stack class 005 unity engine view
详解华为应用市场2022年逐步减少32位包体上架应用和策略
Exercise arrangement 2.10, 11
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
Tips for using jeditabletable
Redis summary
数据库存储---表分区
redis故障处理 “Can‘t save in background: fork: Cannot allocate memory“
Category of IP address
登山小分队(dfs)