★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-anojmbqo-ky.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Return the result of evaluating a given boolean expression
, represented as a string.git
An expression can either be:github
"t"
, evaluating to True
;"f"
, evaluating to False
;"!(expr)"
, evaluating to the logical NOT of the inner expression expr
;"&(expr1,expr2,...)"
, evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...
;"|(expr1,expr2,...)"
, evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...
Example 1:express
Input: expression = "!(f)" Output: true
Example 2:微信
Input: expression = "|(f,t)" Output: true
Example 3:app
Input: expression = "&(t,f)" Output: false
Example 4:lua
Input: expression = "|(&(t,f,t),!(t))" Output: false
Constraints:spa
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
,返回該式的運算結果。code
有效的表達式需遵循如下約定:htm
"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
是以上述形式給出的有效表達式,表示一個布爾值。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 }
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 }