[LeetCode] Parse Lisp Expression 解析Lisp表达式

You are given a string expression representing a Lisp-like expression to return the integer value of.

The syntax for these expressions is given as follows.

  • An expression is either an integer, a let-expression, an add-expression, a mult-expression, or an assigned variable. Expressions always evaluate to a single integer.
  • (An integer could be positive or negative.)
  • A let-expression takes the form (let v1 e1 v2 e2 ... vn en expr), where let is always the string "let", then there are 1 or more pairs of alternating variables and expressions, meaning that the first variable v1 is assigned the value of the expression e1, the second variable v2 is assigned the value of the expression e2, and so on sequentially; and then the value of this let-expression is the value of the expression expr.
  • An add-expression takes the form (add e1 e2) where add is always the string "add", there are always two expressions e1, e2, and this expression evaluates to the addition of the evaluation of e1 and the evaluation of e2.
  • A mult-expression takes the form (mult e1 e2) where mult is always the string "mult", there are always two expressions e1, e2, and this expression evaluates to the multiplication of the evaluation of e1 and the evaluation of e2.
  • For the purposes of this question, we will use a smaller subset of variable names. A variable starts with a lowercase letter, then zero or more lowercase letters or digits. Additionally for your convenience, the names "add", "let", or "mult" are protected and will never be used as variable names.
  • Finally, there is the concept of scope. When an expression of a variable name is evaluated, within the context of that evaluation, the innermost scope (in terms of parentheses) is checked first for the value of that variable, and then outer scopes are checked sequentially. It is guaranteed that every expression is legal. Please see the examples for more details on scope.

Evaluation Examples:

Input: (add 1 2)
Output: 3

Input: (mult 3 (add 2 3))
Output: 15

Input: (let x 2 (mult x 5))
Output: 10

Input: (let x 2 (mult x (let x 3 y 4 (add x y))))
Output: 14
Explanation: In the expression (add x y), when checking for the value of the variable x,
we check from the innermost scope to the outermost in the context of the variable we are trying to evaluate.
Since x = 3 is found first, the value of x is 3.

Input: (let x 3 x 2 x)
Output: 2
Explanation: Assignment in let statements is processed sequentially.

Input: (let x 1 y 2 x (add x y) (add x y))
Output: 5
Explanation: The first (add x y) evaluates as 3, and is assigned to x.
The second (add x y) evaluates as 3+2 = 5.

Input: (let x 2 (add (let x 3 (let x 4 x)) x))
Output: 6
Explanation: Even though (let x 4 x) has a deeper scope, it is outside the context
of the final x in the add-expression.  That final x will equal 2.

Input: (let a1 3 b2 (add a1 1) b2)
Output 4
Explanation: Variable names can contain digits after the first character.

Note:

  • The given string expression is well formatted: There are no leading or trailing spaces, there is only a single space separating different components of the string, and no space between adjacent parentheses. The expression is guaranteed to be legal and evaluate to an integer.
  • The length of expression is at most 2000. (It is also non-empty, as that would not be a legal expression.)
  • The answer and all intermediate calculations of that answer are guaranteed to fit in a 32-bit integer.

s

时间: 2024-11-06 03:49:28

[LeetCode] Parse Lisp Expression 解析Lisp表达式的相关文章

Tomcat 7 'javax.el.ELException' 的解决方式(failed to parse the expression [${xxx}])

Tomcat 7 'javax.el.ELException' 的解决方式 tomcat 7对EL表达式的语法要求比较严格,例如"${owner.new}"因包含关键字new就会导致解析出错. 问题是出来了,怎么解决呢?有三种,如下: 第一种:严格遵守java规范,修改对象的属性名称,要求不包含java关键字; 第二种:修改EL表达式,例如"${owner.new}"可以修改为"${owner['new']}"; 第三种:修改tomcat属性,忽

处理 javax.el.ELException: Failed to parse the expression 报错

在JSP的表达式语言中,使用了  <h3>是否新Session:${pageContext.session.new}</h3>  输出Session是否是新的,此时遇到了  javax.el.ELException: Failed to parse the expression  报错.这里主要是因为在Tomcat7中表达式的权限变小了,如果遇到JAVA的关键字,就会出现此种错误,在这个例子中就是因为 new 是JAVA的关键字,所以才会出错. 解决办法:设置启动参数   -Dor

Spring事务管理—aop:pointcut expression解析

先来看看这个spring的配置文件的配置: <!-- 事务管理器 -->  <bean id="transactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">   <property name="sessionFactory" ref="sessionFactory" /&g

堆栈解析算术表达式

利用栈,解析算术表达式问题就立刻变得容易许多.给出的示例代码能解析任何包括+,-,*,/,()和0到9数字组成的算术表达式. 中缀表达式和后缀表达式中缀表达式就是通常所说的算术表达式,比如(1+2)*3-4.后缀表达式是指通过解析后,运算符在运算数之后的表达式,比如上式解析成后缀表达式就是12+3*4-.这种表达式可以直接利用栈来求解. 运算符的优先级 优先级 运算符 1 括号() 2 负号- 3 乘方** 4 乘*,除/,求余% 5 加+,减- 6 小于<,小于等于<=,大于>,大于等

LeetCode 010 Regular Expression Matching

[题目] Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype shou

python_day04 函数嵌套 名称空间和作用域 闭包 装饰器 迭代器 生成器 列表解析 三元表达式 生成器表达式

本节课重要知识点内容如下: 函数嵌套 名称空间和作用域 闭包 装饰器 迭代器 生成器 列表解析 三元表达式 生成器表达式 1.函数嵌套 函数的嵌套调用:在调用一个函数的过程中,又调用了其他函数函数的嵌套定义:在一个函数的内部,又定义另外一个函数 def bar(): print('from nbar')def foo(): print('from foo') bar()foo()def max2(x,y): if x > y: return x else: return ydef max4(a,

使用栈实现解析算术表达式

目的 1. 使用栈将中缀表达式转换成后缀表达式 2. 使用后缀表达式求算术值 注意: 因为是简单实践,所以代码逻辑已经简化,比如只能对个位数的加减乘除进行解析.没有设异常处理等 一:需要实现一个栈 这个没什么好说的,只是一个结构很简单的栈 1 public class Stack { 2 3 private int maxSize; 4 private int top; 5 private Object[] stackArr; 6 7 public Stack(int maxSize) { 8

数据结构 栈解析 算法表达式

1 本文目标分析用堆栈解析算术表达式的基本方法.给出的示例代码能解析任何包括+,-,*,/,()和0到9数字组成的算术表达式. 2 中缀表达式和后缀表达式 中缀表达式就是通常所说的算术表达式,比如(1+2)*3-4. 后缀表达式是指通过解析后,运算符在运算数之后的表达式,比如上式解析成后缀表达式就是12+3*4-.这种表达式可以直接利用栈来求解. 3 运算符的优先级 优先级 运算符 1 括号() 2 负号- 3 乘方** 4 乘*,除/,求余% 5 加+,减- 6 小于<,小于等于<=,大于&

在Maven项目中,jsp不解析el表达式

我的这个项目是用Maven-archetype-webapp项目创建的,如下图所示: 有这种方式创建有一个坑,就是它使用的servlet版本是2.3,而servlet2.4以下的版本是不会自动解析el表达式的,自动创建的web.xml的头如下所示: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-a