当前位置:网站首页>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;
}
}
边栏推荐
- opencv之图像分割
- 如何在图片的目标中添加目标的mask
- QT charts use (rewrite qchartview to realize some custom functions)
- Sign and authenticate API interface or H5 interface
- Greenplum6.x常用语句
- 如何在快应用中实现滑动操作组件
- Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
- Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - package based on thinkphp5 - detailed guidance of the author - July 6, 2022 - Novice
- Uniapp wechat applet monitoring network
- Required String parameter ‘XXX‘ is not present
猜你喜欢

路由信息协议——RIP

Category of IP address

ncs成都新電面試經驗
![[MySQL] detailed explanation of trigger content of database advanced](/img/6c/8aad649e4ba1160db3aea857ecf4a1.png)
[MySQL] detailed explanation of trigger content of database advanced

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

Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error

leetcode135. Distribute candy

平台化,强链补链的一个支点

idea里使用module项目的一个bug

Interpolation lookup (two methods)
随机推荐
Greenplum6.x搭建_环境配置
Greenplum 6.x monitoring software setup
Introduction to data fragmentation
Why choose cloud native database
Greenplum6.x重新初始化
数据分片介绍
Uniapp wechat applet monitoring network
【微信小程序:缓存操作】
Greenplum 6.x common statements
Data analysis methodology and previous experience summary 2 [notes dry goods]
Arm GIC (IV) GIC V3 register class analysis notes.
Three usage scenarios of annotation @configurationproperties
基本数据类型和string类型互相转化
如何在图片的目标中添加目标的mask
Shell script for changing the current folder and the file date under the folder
National SMS center number inquiry
更改当前文件夹及文件夹下文件日期shell脚本
What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
selenium自动化集成,八年测试经验软测工程师,一篇文章带你学懂