/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 返回表达式的值
* @param s string字符串 待计算的表达式
* @return int整型
*/
func solve( s string ) int {
i := 0
var helper func(string) int
helper = func (s string) int {
sign := byte('+')
num := 0
bytes := []byte(s)
stack := []int{}
for i < len(bytes) {
b := s[i]
i++
if isDigit(b) {
num = num * 10 + int(b-'0')
}
if b == '(' {
num = helper(s)
}
if !isDigit(b) && b != ' ' || i == len(bytes) {
switch sign {
case '+' :
stack = append(stack, num)
case '-' :
stack = append(stack, -num)
case '*' :
stack[len(stack)-1] *= num
case '/' :
stack[len(stack)-1] /= num
}
sign = b
num = 0
}
if b == ')' {
break
}
}
return sum(stack)
}
return helper(s)
}
func isDigit(b byte) bool {
return b >= '0' && b <= '9'
}
func sum(stack []int) int {
s := 0
for _, num := range stack {
s += num
}
return s
}