Skip to content

Add solution and test-cases for problem 1106 #935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
62 changes: 60 additions & 2 deletions leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading