forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1106.go
51 lines (47 loc) · 871 Bytes
/
1106.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var s []byte
func parseBoolExpr(expression string) bool {
s = []byte(expression)
i := 0
return parseStr(&i)
}
func parseStr(i *int) bool {
if s[*i] == 't' {
(*i)++
return true
} else if s[*i] == 'f' {
(*i)++
return false
} else if s[*i] == '&' {
return parseAnd(i)
} else if s[*i] == '|' {
return parseOr(i)
} else {
return parseNot(i)
}
}
func parseAnd(i *int) bool {
(*i) += 2
res := parseStr(i)
for s[*i] != ')' {
(*i)++
res = parseStr(i) && res
}
(*i)++
return res
}
func parseOr(i *int) bool {
(*i) += 2
res := parseStr(i)
for s[*i] != ')' {
(*i)++
res = parseStr(i) || res
}
(*i)++
return res
}
func parseNot(i *int) bool {
(*i) += 2
res := !parseStr(i)
(*i)++
return res
}