forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0224.go
More file actions
27 lines (27 loc) · 685 Bytes
/
0224.go
File metadata and controls
27 lines (27 loc) · 685 Bytes
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
func calculate(s string) int {
st := make([]int, 0)
op, l, r := 1, 0, 0
for _, c := range s {
if c >= '0' && c <= '9' {
r = r * 10 + (int(c) - 48)
} else if c == '+' || c == '-' {
l += op * r
r = 0
if c == '+' {
op = 1
} else {
op = -1
}
} else if c == '(' {
st = append(st, l)
st = append(st, op)
op, l = 1, 0
} else if c == ')' {
l += op * r
r = 0
l = st[len(st) - 1] * l + st[len(st) - 2]
st = st[:len(st) - 2]
}
}
return l + op * r
}