[Swift]LeetCode1106. 解析布尔表达式 | Parsing A Boolean Expression
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11096570.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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: true
Example 2:
Input: expression = "|(f,t)" Output: true
Example 3:
Input: expression = "&(t,f)" Output: false
Example 4:
Input: expression = "|(&(t,f,t),!(t))" Output: false
Constraints:
1 <= expression.length <= 20000
expression[i]
consists of characters in{'(', ')', '&', '|', '!', 't', 'f', ','}
.expression
is a valid expression representing a boolean, as given in the description.
给你一个以字符串形式表述的 布尔表达式(boolean) expression
,返回该式的运算结果。
有效的表达式需遵循以下约定:
"t"
,运算结果为True
"f"
,运算结果为False
"!(expr)"
,运算过程为对内部表达式expr
进行逻辑 非的运算(NOT)"&(expr1,expr2,...)"
,运算过程为对 2 个或以上内部表达式expr1, expr2, ...
进行逻辑 与的运算(AND)"|(expr1,expr2,...)"
,运算过程为对 2 个或以上内部表达式expr1, expr2, ...
进行逻辑 或的运算(OR)
示例 1:
输入:expression = "!(f)" 输出:true
示例 2:
输入:expression = "|(f,t)" 输出:true
示例 3:
输入:expression = "&(t,f)" 输出:false
示例 4:
输入:expression = "|(&(t,f,t),!(t))" 输出:false
提示:
1 <= expression.length <= 20000
expression[i]
由{'(', ')', '&', '|', '!', 't', 'f', ','}
中的字符组成。expression
是以上述形式给出的有效表达式,表示一个布尔值。
Runtime: 32 ms
Memory Usage: 20.8 MB
1 class Solution { 2 func parseBoolExpr(_ expression: String) -> Bool { 3 var op:[Character] = [Character]() 4 var num:[Int] = [Int]() 5 for ch in expression 6 { 7 if ch == "t" {num.append(1)} 8 else if ch == "f" {num.append(0)} 9 else if (ch == "|") || (ch == "&") || (ch == "!") {op.append(ch)} 10 else if ch == "(" {num.append(-1)} 11 else if ch == "," {continue} 12 else if ch == ")" 13 { 14 var ans:Int = num.popLast()! 15 let p:Character = op.popLast()! 16 while(num.last! != -1) 17 { 18 let a:Int = num.popLast()! 19 if p == "&" {ans = ans & a} 20 else if p == "|" {ans = ans | a} 21 } 22 num.popLast() 23 if p == "!" {ans = 1 - ans} 24 num.append(ans) 25 } 26 } 27 if num.last! == 1 28 { 29 return true 30 } 31 return false 32 } 33 }
72ms
1 class Solution { 2 var expressions : [Character] = ["&","|","!"] 3 var bools : [Character] = ["t","f"] 4 func parseBoolExpr(_ expression: String) -> Bool { 5 var stack : String = "" 6 let expression = Array(expression) 7 8 _ = true 9 var currentBools = [Character]() 10 for i in 0..<expression.count { 11 12 if expression[i] == ")" { 13 while let head = stack.popLast(), head != "(" { 14 if head == "t" || head == "f" { 15 currentBools.append(head) 16 } 17 } 18 if let op = stack.popLast() { 19 if op == "&" { 20 stack.append(evaluateAND(currentBools)) 21 currentBools.removeAll() 22 } else if op == "|" { 23 stack.append(evaluateOR(currentBools)) 24 currentBools.removeAll() 25 } else if op == "!" { 26 stack.append(evaluateNot(currentBools.first!)) 27 currentBools.removeAll() 28 } 29 continue 30 } 31 } 32 if bools.contains(expression[i]) { 33 currentBools.append(expression[i]) 34 continue 35 } 36 37 stack.append(expression[i]) 38 } 39 if stack.popLast() == "t" {return true} 40 return false 41 } 42 43 func evaluateOR (_ input: [Character]) -> Character { 44 if input.contains("t") {return "t"} 45 return "f" 46 } 47 48 func evaluateAND (_ input: [Character]) -> Character { 49 if input.contains("f") {return "f"} 50 return "t" 51 } 52 53 func evaluateNot(_ input: Character) -> Character { 54 if input == ("f") {return "t"} 55 return "f" 56 } 57 }