当前位置:网站首页>LeetCode 736. Lisp 语法解析
LeetCode 736. Lisp 语法解析
2022-07-07 06:14:00 【Sasakihaise_】


【DFS】字符串解析+递归
首先提取出操作符后,后面按照子表达式的概念进行拆分,拆分后放在一个list中。
如果操作符为let,那么两两遍历,第一个字符串作为key,第二个字符串是一个表达式,继续递归解析,将返回值作为value一起存入map中。然后返回最后一个字符串的解析值。
如果操作符为add,那么只需要将两个表达式的解析结果相加返回。
操作符为mult同理。
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;
}
}
边栏推荐
- [hard core science popularization] working principle of dynamic loop monitoring system
- [kuangbin] topic 15 digit DP
- Iptables' state module (FTP service exercise)
- 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
- Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
- All about PDF crack, a complete solution to meet all your PDF needs
- Qt Charts使用(重写QChartView,实现一些自定义功能)
- 【MySQL】数据库进阶之触发器内容详解
- leetcode135. Distribute candy
- POJ - 3616 Milking Time(DP+LIS)
猜你喜欢
![Upload an e-office V9 arbitrary file [vulnerability recurrence practice]](/img/e7/278193cbc2a2f562270f99634225bc.jpg)
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]

Greenplum6.x-版本变化记录-常用手册

Novice entry SCM must understand those things

Exercise arrangement 2.10, 11

调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error

MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)

調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error

Rapid integration of authentication services - harmonyos platform

How to integrate app linking services in harmonyos applications

In go language, function is a type
随机推荐
FPGA knowledge accumulation [6]
In go language, function is a type
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
[Nanjing University] - [software analysis] course learning notes (I) -introduction
Greenplum 6.x build_ Environment configuration
GOLand idea intellij 无法输入汉字
[wechat applet: cache operation]
[kuangbin] topic 15 digit DP
2-3查找樹
为什么要选择云原生数据库
Three series of BOM elements
Sign and authenticate API interface or H5 interface
Data type - integer (C language)
Greenplum6.x-版本变化记录-常用手册
ncs成都新电面试经验
[step on the pit] Nacos registration has been connected to localhost:8848, no available server
IP地址的类别
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
NCS Chengdu Xindian interview experience
POJ - 3616 Milking Time(DP+LIS)