当前位置:网站首页>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;
}
}
边栏推荐
- uniapp 微信小程序监测网络
- Three series of BOM elements
- Greenplum 6.x common statements
- Teach you how to select PCB board by hand (II)
- 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
- Rapid integration of authentication services - harmonyos platform
- 对API接口或H5接口做签名认证
- leetcode135. Distribute candy
- IP guard helps energy enterprises improve terminal anti disclosure measures to protect the security of confidential information
- Greenplum 6.x version change record common manual
猜你喜欢
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
leetcode135. Distribute candy
In go language, function is a type
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
详解华为应用市场2022年逐步减少32位包体上架应用和策略
oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
IP地址的类别
Category of IP address
let const
随机推荐
Golan idea IntelliJ cannot input Chinese characters
Greenplum6.x监控软件搭建
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
Greenplum6.x搭建_安装
[machine learning] watermelon book data set_ data sharing
Three series of BOM elements
Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
路由信息协议——RIP
uniapp 微信小程序监测网络
[hard core science popularization] working principle of dynamic loop monitoring system
Leetcode 1984. Minimum difference in student scores
[step on the pit] Nacos registration has been connected to localhost:8848, no available server
Greenplum6.x搭建_环境配置
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
Three usage scenarios of annotation @configurationproperties
Shell script for changing the current folder and the file date under the folder
登山小分队(dfs)
String operation
2-3 lookup tree
Greenplum 6.x version change record common manual