当前位置:网站首页>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;
}
}
边栏推荐
- Leetcode 1984. Minimum difference in student scores
- National SMS center number inquiry
- 2-3 lookup tree
- uniapp 微信小程序监测网络
- Uniapp wechat applet monitoring network
- 字符串操作
- 調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
- Gson converts the entity class to JSON times declare multiple JSON fields named
- 详解华为应用市场2022年逐步减少32位包体上架应用和策略
- 关于基于kangle和EP面板使用CDN
猜你喜欢
Greenplum6.x-版本变化记录-常用手册
leetcode135. Distribute candy
指针进阶,字符串函数
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
Greenplum6.x监控软件搭建
Data type - integer (C language)
How to integrate app linking services in harmonyos applications
FPGA knowledge accumulation [6]
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
Compilation and linking of programs
随机推荐
2-3 lookup tree
Opencv converts 16 bit image data to 8 bits and 8 to 16
Implementation method of data platform landing
mysql分区讲解及操作语句
Virtual address space
Greenplum 6.x reinitialization
uniapp 微信小程序监测网络
Tips for using jeditabletable
Download and install orcale database11.2.0.4
[Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
Golan idea IntelliJ cannot input Chinese characters
Greenplum6.x-版本变化记录-常用手册
POJ - 3616 Milking Time(DP+LIS)
Greenplum6.x常用语句
Shell script for changing the current folder and the file date under the folder
In go language, function is a type
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
Data type - floating point (C language)
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
Frequently Asked Coding Problems