【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- Eclipse设置保存时自动格式化代码
在使用eclipse时,经常需要使用到Ctrl+Shift+F来格式化代码,可以打开保存时格式化,会更方便. 打开方式:Window-->Preferences-->Java --> ...
- DOM4J解析文件
转发一篇好文 DOM4J解析文件 Dom4j和Xpath
- mount挂载相关指令
最近需要重新挂载一块数据盘,增加挂载设置,遇到一些问题做下记录. step1:df -h 或 lsblk 查看分区挂载和对应挂载的目录 /dev/xxx /data step2:umount /dev ...
- LeetCode算法题-Most Common Word(Java实现)
这是悦乐书的第321次更新,第342篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第190题(顺位题号是819).给定一个段落和一组禁止词,返回不在禁止词列表中的最常用词 ...
- VMware克隆虚拟机后mac地址重新设置
ifconfig eth1 确定新网卡的MAC地址. nmcli con 确定新网卡的UUID vim /etc/udev/rules.d/70-persistent-net.rules 把原et ...
- 国家授时中心的NTP服务器地址 210.72.145.44
国家授时中心的NTP服务器地址 210.72.145.44
- JDK,JRE,JVM的区别与联系?
概念区别 JDK: Java Develpment Kit java 开发工具JRE: Java Runtime Environment java运行时环境JVM: ...
- Java GC日志
JVM 命令:-Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseSerialGC [GC (Allocatio ...
- P2010回文日期
这道题是2016年普及组的题,难度等级为普及-. 这道题仍然是个模拟题.有两种策略:1.枚举回文,看日期是否存在2.枚举日期,看是否是回文.显然,前者要快很多,并且准确.本蒟蒻第一次便使用了后者,bu ...
- Vim实用技巧(一)
vim 命令按键规定 标记 含义 x 按一次 x dw 按一次 d, w dap 按一次 d, a, p 同时按 和 n g<C-]> 按 g, 然后同时按 和 ] <C-=> ...