diff --git a/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/README.md b/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/README.md index 80d3690ff..2085bbf61 100644 --- a/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/README.md +++ b/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/README.md @@ -1,28 +1,46 @@ # [1106.Parsing A Boolean Expression][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +A **boolean expression** is an expression that evaluates to either `true` or `false`. It can be in one of the following shapes: + +- `'t'` that evaluates to `true`. +- `'f'` that evaluates to `false`. +- `'!(subExpr)'` that evaluates to **the logical NOT** of the inner expression `subExpr`. +- `'&(subExpr1, subExpr2, ..., subExprn)'` that evaluates to **the logical AND** of the inner expressions `subExpr1, subExpr2, ..., subExprn` where `n >= 1`. +- `'|(subExpr1, subExpr2, ..., subExprn)'` that evaluates to **the logical OR** of the inner expressions `subExpr1, subExpr2, ..., subExprn` where `n >= 1`. + +Given a string `expression` that represents a **boolean expression**, return the evaluation of that expression. + +It is **guaranteed** that the given expression is valid and follows the given rules. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: expression = "&(|(f))" +Output: false +Explanation: +First, evaluate |(f) --> f. The expression is now "&(f)". +Then, evaluate &(f) --> f. The expression is now "f". +Finally, return false. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Parsing A Boolean Expression -```go ``` +Input: expression = "|(f,f,f,t)" +Output: true +Explanation: The evaluation of (false OR false OR false OR true) is true. +``` + +**Example 3:** +``` +Input: expression = "!(&(f,t))" +Output: true +Explanation: +First, evaluate &(f,t) --> (false AND true) --> false --> f. The expression is now "!(f)". +Then, evaluate !(f) --> NOT false --> true. We return true. +``` ## 结语 diff --git a/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/Solution.go b/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/Solution.go index d115ccf5e..e288dba3c 100644 --- a/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/Solution.go +++ b/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/Solution.go @@ -1,5 +1,63 @@ package Solution -func Solution(x bool) bool { - return x +type tmp1106 struct { + op byte + + f, t bool +} + +func Solution(expression string) bool { + l := len(expression) + stack := make([]tmp1106, l) + index := -1 + f, t := false, false + i := 0 + for ; i < l; i++ { + cur := expression[i] + if cur == '!' || cur == '|' || cur == '&' { + index++ + stack[index] = tmp1106{ + cur, f, t, + } + t, f = false, false + i++ + continue + } + if cur == ')' { + top := stack[index] + index-- + if top.op == '!' { + if t { + t, f = false, true + f = true + } else { + t, f = true, false + } + } + if top.op == '|' { + if t { + t, f = true, false + } else { + t, f = false, true + } + } + if top.op == '&' { + if f { + t, f = false, true + } else { + t, f = true, false + } + } + t = t || top.t + f = f || top.f + continue + } + if cur == 'f' { + f = true + } + if cur == 't' { + t = true + } + } + return t } diff --git a/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/Solution_test.go b/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/Solution_test.go index 14ff50eb4..218e84e84 100644 --- a/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/Solution_test.go +++ b/leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/Solution_test.go @@ -10,12 +10,13 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs string expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "&(|(f))", false}, + {"TestCase2", "|(f,f,f,t)", true}, + {"TestCase3", "!(&(f,t))", true}, + {"TestCase4", "|(&(t,f,t),!(t))", false}, } // 开始测试 @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }