当前位置:网站首页>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;
}
}
边栏推荐
- Compilation and linking of programs
- let const
- [Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
- Introduction to data fragmentation
- Iptables' state module (FTP service exercise)
- String operation
- 登山小分队(dfs)
- Gson转换实体类为json时报declares multiple JSON fields named
- Data type - floating point (C language)
- Greenplum 6.x build_ install
猜你喜欢
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
路由信息协议——RIP
Teach you how to select PCB board by hand (II)
leetcode135. Distribute candy
2-3 lookup tree
Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
数字三角形模型 AcWing 275. 传纸条
随机推荐
Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
Componentspace2022, assertions, protocols, bindings, and configuration files
Database storage - table partition
2 - 3 arbre de recherche
Required String parameter ‘XXX‘ is not present
2-3查找樹
2-3 lookup tree
求有符号数的原码、反码和补码【C语言】
ncs成都新電面試經驗
Redis summary
National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
ncs成都新电面试经验
详解华为应用市场2022年逐步减少32位包体上架应用和策略
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?
注解@ConfigurationProperties的三种使用场景
Go write a program that runs within a certain period of time
Exercise arrangement 2.10, 11
Rapid integration of authentication services - harmonyos platform
opencv 将16位图像数据转为8位、8转16