【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- Selenium学习之==>Selenium介绍
前世 Selenium RC 早期的Selenium使用的是JavaScript注入技术与浏览器打交道,需要Selenium RC启动一个Server,将操作Web元素的API调用转化为一段段Java ...
- django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.的解决办法
如题,这个错误的解决办法如下: 在代码文件的最上方添加以下代码: import os,django os.environ.setdefault("DJANGO_SETTINGS_MODULE ...
- 中国MOOC_零基础学Java语言_第6周 使用对象_2GPS数据处理
2 GPS数据处理(5分) 题目内容: NMEA-0183协议是为了在不同的GPS(全球定位系统)导航设备中建立统一的BTCM(海事无线电技术委员会)标准,由美国国家海洋电子协会(NMEA-The N ...
- 手把手教你搭建一个 Elasticsearch 集群
为何要搭建 Elasticsearch 集群 凡事都要讲究个为什么.在搭建集群之前,我们首先先问一句,为什么我们需要搭建集群?它有什么优势呢? 高可用性 Elasticsearch 作为一个搜索引擎, ...
- 如何用katalon录制回放一个web UI测试—— katalon学习笔记(四)
,首先打开katanlon,进入到katalon主界面,选择点击file->new->project ,在创建新项目弹出框中Name输入项输入项目的名称:Type选择web,也就是你要测试 ...
- java 中的equals()小结
转载自http://www.cnblogs.com/jackyrong/archive/2006/08/20/481994.html Java中的equals是十分重要的,和= =要区别开来,最近在看 ...
- Java中字母大小写的转换
例:String str = "AbC"; 把字符串所有字母变成小写: System.out.println(str.toLowerCase()); 把字符串所有字母大写: Sys ...
- [DS+Algo] 004 栈、队列及其代码实现
1. Stack FILO (FirstInLastOut) 的链表结构 在程序编译环境上使用较多 常用操作 push pop peek is_empty size Python 代码示例 class ...
- selectnodes和selectSingleNode
selectnodes: selectNodes和ChildNodes获取XML内容数组的差异性 我们在使用XML进行查询或者变更数据的时候,需要注意两个很相近但结果相差很大的用法,如下: 1: Xm ...
- HNUSTOJ-1639 分糖果(几何)
1639: 分糖果 时间限制: 1 Sec 内存限制: 128 MB提交: 261 解决: 118[提交][状态][讨论版] 题目描述 为了实验室的发展,吴大大采购了一箱零食O(∩_∩)O~~ 在 ...