【leetcode】1106. Parsing A Boolean Expression
题目如下:
Return the result of evaluating a given boolean
expression, represented as a string.An expression can either be:
"t", evaluating toTrue;"f", evaluating toFalse;"!(expr)", evaluating to the logical NOT of the inner expressionexpr;"&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressionsexpr1, expr2, ...;"|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressionsexpr1, expr2, ...Example 1:
Input: expression = "!(f)"
Output: trueExample 2:
Input: expression = "|(f,t)"
Output: trueExample 3:
Input: expression = "&(t,f)"
Output: falseExample 4:
Input: expression = "|(&(t,f,t),!(t))"
Output: falseConstraints:
1 <= expression.length <= 20000expression[i]consists of characters in{'(', ')', '&', '|', '!', 't', 'f', ','}.expressionis a valid expression representing a boolean, as given in the description.
解题思路:本题和表达式运算的题目相似。遍历expression并将每一个字符依次入栈,如果遇到')',则找出离栈顶最近的'(',计算出括号之内的表达式的值并将该值入栈,直到expression遍历完成为止。
代码如下:
class Solution(object):
def parseBoolExpr(self, expression):
"""
:type expression: str
:rtype: bool
"""
stack = []
expression = expression.replace('t','')
expression = expression.replace('f', '')
ex = list(expression)
while len(ex) > 0:
char = ex.pop(0)
if char != ')':
stack.append(char)
continue
ts = ''
while len(stack) > 0:
item = stack.pop(-1)
if item == '(':
break
ts += item
ts_list = ts.split(',')
and_or = stack.pop(-1)
if and_or == '!':
stack.append('' if ts_list[0] == '' else '' )
elif and_or == '|':
stack.append('' if '' in ts_list else '')
else:
stack.append('' if '' in ts_list else '')
return stack[0] == ''
【leetcode】1106. Parsing A Boolean Expression的更多相关文章
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)
[LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- What is the difference between a URI, a URL and a URN?
资料: URI: URL: URN:
- 【MyBatis】-----初识【MyBatis】
一.核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration ...
- 8.FTP后门命令执行----Samba命令执行----VMware安装kali----多终端显示
FTP后门命令执行 再次声明,最近听闻不得教受工具使用等言论. 我敢打包票,网络空间安全一级学科的老师和学生是不会说这句话的.未知攻,焉知防. 有工具来检测自己和玩弄它,是幸运的. 犯罪者不会给你提供 ...
- ActiveMQ学习教程/1.简要介绍与安装
ActiveMQ学习教程(一)——简要介绍与安装 一.名词: 1.JMS:即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的 ...
- 使用itchat获取微信好友的男女比例
# 使用itchat获取微信好友的男女比例 import itchat itchat.auto_login(hotReload=True) friends = itchat.get_friends(u ...
- Java简易实现记事本的打开与保存
记事本的打开与保存 一些总结 * Swing中有时方法不显示,需要把方setVisible(true)放到最后执行 * AWT中的TextArea默认是中间布局 * fileDialog对话框Load ...
- 配置NAT
NAT是将IP数据报文报头中的IP地址转换为另-一个IP地址的过程,主要用于实现内部网络(私有IP地址)访问外部网络(公有IP地址)的功能.NAT有3种类型:静态NAT.动态地址NAT以及网络地址端口 ...
- dotnet sdk 的镜像tag 相关
https://hub.docker.com/_/microsoft-dotnet-core-sdk/ 微软的dotnet sdk 的 tag 微软貌似改默认镜像 dockerhub 里面的tag与 ...
- Field baseMapper in com.baomidou.mybatisplus.extension.service.impl.ServiceImpl required a single bean, but xx were found:
在学习使用 mybatis-plus 时,遇到一个奇怪的异常 如 代码一: 代码一: Error starting ApplicationContext. To display the conditi ...
- 使用Object.create()实现继承 用 Object.create实现类式继承
使用Object.create()实现继承:https://www.cnblogs.com/cuew1987/p/4075027.html 用 Object.create实现类式继承:https:// ...