当前位置:网站首页>leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】
leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】
2022-07-06 18:37:00 【白速龙王的回眸】

分析
不看逻辑看知识
Enum + auto
设置固定id的状态,可以理解为“类的静态状态变量”
用于判断和标识独有的状态,可用is判断
solts
关于solts的用法,也是可以理解为“类的静态变量”

类一旦做一个修改,所有的实例的值都会变
ac code
from enum import Enum, auto
class ExprStatus(Enum):
VALUE = auto() # 初始状态
NONE = auto() # 表达式类型未知
LET = auto() # let 表达式
LET1 = auto() # let 表达式已经解析了 vi 变量
LET2 = auto() # let 表达式已经解析了最后一个表达式 expr
ADD = auto() # add 表达式
ADD1 = auto() # add 表达式已经解析了 e1 表达式
ADD2 = auto() # add 表达式已经解析了 e2 表达式
MULT = auto() # mult 表达式
MULT1 = auto() # mult 表达式已经解析了 e1 表达式
MULT2 = auto() # mult 表达式已经解析了 e2 表达式
DONE = auto() # 解析完成
class Expr:
#__slots__ = 'status', 'var', 'value', 'e1', 'e2'
def __init__(self, status):
self.status = status
self.var = '' # let 的变量 vi
self.value = 0 # VALUE 状态的数值,或者 LET2 状态最后一个表达式的数值
self.e1 = self.e2 = 0 # add 或 mult 表达式的两个表达式 e1 和 e2 的数值
class Solution:
def evaluate(self, expression: str) -> int:
scope = defaultdict(list)
def calculateToken(token: str) -> int:
return scope[token][-1] if token[0].islower() else int(token)
vars = []
s = []
cur = Expr(ExprStatus.VALUE)
i, n = 0, len(expression)
while i < n:
if expression[i] == ' ':
i += 1 # 去掉空格
continue
if expression[i] == '(':
i += 1 # 去掉左括号
s.append(cur)
cur = Expr(ExprStatus.NONE)
continue
if expression[i] == ')': # 本质上是把表达式转成一个 token
i += 1 # 去掉右括号
if cur.status is ExprStatus.LET2:
token = str(cur.value)
for var in vars[-1]:
scope[var].pop() # 清除作用域
vars.pop()
elif cur.status is ExprStatus.ADD2:
token = str(cur.e1 + cur.e2)
else:
token = str(cur.e1 * cur.e2)
cur = s.pop() # 获取上层状态
else:
i0 = i
while i < n and expression[i] != ' ' and expression[i] != ')':
i += 1
token = expression[i0:i]
if cur.status is ExprStatus.VALUE:
cur.value = int(token)
cur.status = ExprStatus.DONE
elif cur.status is ExprStatus.NONE:
if token == "let":
cur.status = ExprStatus.LET
vars.append([]) # 记录该层作用域的所有变量, 方便后续的清除
elif token == "add":
cur.status = ExprStatus.ADD
elif token == "mult":
cur.status = ExprStatus.MULT
elif cur.status is ExprStatus.LET:
if expression[i] == ')': # let 表达式的最后一个 expr 表达式
cur.value = calculateToken(token)
cur.status = ExprStatus.LET2
else:
cur.var = token
vars[-1].append(token) # 记录该层作用域的所有变量, 方便后续的清除
cur.status = ExprStatus.LET1
elif cur.status is ExprStatus.LET1:
scope[cur.var].append(calculateToken(token))
cur.status = ExprStatus.LET
elif cur.status is ExprStatus.ADD:
cur.e1 = calculateToken(token)
cur.status = ExprStatus.ADD1
elif cur.status is ExprStatus.ADD1:
cur.e2 = calculateToken(token)
cur.status = ExprStatus.ADD2
elif cur.status is ExprStatus.MULT:
cur.e1 = calculateToken(token)
cur.status = ExprStatus.MULT1
elif cur.status is ExprStatus.MULT1:
cur.e2 = calculateToken(token)
cur.status = ExprStatus.MULT2
return cur.value
总结
时隔几个月
又找到一道我连题解都不想看的每日一题
nice
边栏推荐
- Centros 8 installation MySQL Error: The gpg Keys listed for the "MySQL 8.0 Community Server" repository are already ins
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
- Get to know MySQL for the first time
- Web开发小妙招:巧用ThreadLocal规避层层传值
- String to date object
- Shortcut keys commonly used in idea
- The foreground downloads network pictures without background processing
- How to use strings as speed templates- How to use String as Velocity Template?
- 新一代云原生消息队列(一)
- UC伯克利助理教授Jacob Steinhardt预测AI基准性能:AI在数学等领域的进展比预想要快,但鲁棒性基准性能进展较慢
猜你喜欢

【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件

centos8安装mysql报错:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins

@Before, @after, @around, @afterreturning execution sequence

Flir Blackfly S USB3 工业相机:白平衡设置方法

Decryption function calculates "task state and lifecycle management" of asynchronous task capability

机器人队伍学习方法,实现8.8倍的人力回报

The last line of defense of cloud primary mixing department: node waterline design

Dall-E Mini的Mega版本模型发布,已开放下载

Scenario practice: quickly build wordpress blog system based on function calculation

CISP-PTE之命令注入篇
随机推荐
Threadlocalutils (tool class IV)
使用Ceres进行slam必须要弄清楚的几个类和函数
Input and output of C language pointer to two-dimensional array
String to date object
npm install 编译时报“Cannot read properties of null (reading ‘pickAlgorithm‘)“
Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
Get to know MySQL for the first time
Chang'an chain learning notes - certificate model of certificate research
机器人队伍学习方法,实现8.8倍的人力回报
Correct use of BigDecimal
Web开发小妙招:巧用ThreadLocal规避层层传值
【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
#yyds干货盘点# 解决名企真题:最大差值
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
微服务架构介绍
红外相机:巨哥红外MAG32产品介绍
@Before, @after, @around, @afterreturning execution sequence
LeetCode. Sword finger offer 62 The last remaining number in the circle
【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
STM32F4---通用定时器更新中断