Skip to content

Commit 44ff323

Browse files
authored
Merge pull request #935 from 0xff-dev/1106
Add solution and test-cases for problem 1106
2 parents 92f5a58 + 3cc9b68 commit 44ff323

File tree

3 files changed

+98
-21
lines changed

3 files changed

+98
-21
lines changed

leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
# [1106.Parsing A Boolean Expression][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
A **boolean expression** is an expression that evaluates to either `true` or `false`. It can be in one of the following shapes:
5+
6+
- `'t'` that evaluates to `true`.
7+
- `'f'` that evaluates to `false`.
8+
- `'!(subExpr)'` that evaluates to **the logical NOT** of the inner expression `subExpr`.
9+
- `'&(subExpr1, subExpr2, ..., subExprn)'` that evaluates to **the logical AND** of the inner expressions `subExpr1, subExpr2, ..., subExprn` where `n >= 1`.
10+
- `'|(subExpr1, subExpr2, ..., subExprn)'` that evaluates to **the logical OR** of the inner expressions `subExpr1, subExpr2, ..., subExprn` where `n >= 1`.
11+
12+
Given a string `expression` that represents a **boolean expression**, return the evaluation of that expression.
13+
14+
It is **guaranteed** that the given expression is valid and follows the given rules.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: expression = "&(|(f))"
20+
Output: false
21+
Explanation:
22+
First, evaluate |(f) --> f. The expression is now "&(f)".
23+
Then, evaluate &(f) --> f. The expression is now "f".
24+
Finally, return false.
1325
```
1426

15-
## 题意
16-
> ...
17-
18-
## 题解
27+
**Example 2:**
1928

20-
### 思路1
21-
> ...
22-
Parsing A Boolean Expression
23-
```go
2429
```
30+
Input: expression = "|(f,f,f,t)"
31+
Output: true
32+
Explanation: The evaluation of (false OR false OR false OR true) is true.
33+
```
34+
35+
**Example 3:**
2536

37+
```
38+
Input: expression = "!(&(f,t))"
39+
Output: true
40+
Explanation:
41+
First, evaluate &(f,t) --> (false AND true) --> false --> f. The expression is now "!(f)".
42+
Then, evaluate !(f) --> NOT false --> true. We return true.
43+
```
2644

2745
## 结语
2846

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type tmp1106 struct {
4+
op byte
5+
6+
f, t bool
7+
}
8+
9+
func Solution(expression string) bool {
10+
l := len(expression)
11+
stack := make([]tmp1106, l)
12+
index := -1
13+
f, t := false, false
14+
i := 0
15+
for ; i < l; i++ {
16+
cur := expression[i]
17+
if cur == '!' || cur == '|' || cur == '&' {
18+
index++
19+
stack[index] = tmp1106{
20+
cur, f, t,
21+
}
22+
t, f = false, false
23+
i++
24+
continue
25+
}
26+
if cur == ')' {
27+
top := stack[index]
28+
index--
29+
if top.op == '!' {
30+
if t {
31+
t, f = false, true
32+
f = true
33+
} else {
34+
t, f = true, false
35+
}
36+
}
37+
if top.op == '|' {
38+
if t {
39+
t, f = true, false
40+
} else {
41+
t, f = false, true
42+
}
43+
}
44+
if top.op == '&' {
45+
if f {
46+
t, f = false, true
47+
} else {
48+
t, f = true, false
49+
}
50+
}
51+
t = t || top.t
52+
f = f || top.f
53+
continue
54+
}
55+
if cur == 'f' {
56+
f = true
57+
}
58+
if cur == 't' {
59+
t = true
60+
}
61+
}
62+
return t
563
}

leetcode/1101-1200/1106.Parsing-A-Boolean-Expression/Solution_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "&(|(f))", false},
17+
{"TestCase2", "|(f,f,f,t)", true},
18+
{"TestCase3", "!(&(f,t))", true},
19+
{"TestCase4", "|(&(t,f,t),!(t))", false},
1920
}
2021

2122
// 开始测试
@@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)