mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
Add solution 43、45、73、227
This commit is contained in:
@ -0,0 +1,29 @@
|
||||
package leetcode
|
||||
|
||||
func calculate(s string) int {
|
||||
stack, preSign, num, res := []int{}, '+', 0, 0
|
||||
for i, ch := range s {
|
||||
isDigit := '0' <= ch && ch <= '9'
|
||||
if isDigit {
|
||||
num = num*10 + int(ch-'0')
|
||||
}
|
||||
if !isDigit && ch != ' ' || i == len(s)-1 {
|
||||
switch preSign {
|
||||
case '+':
|
||||
stack = append(stack, num)
|
||||
case '-':
|
||||
stack = append(stack, -num)
|
||||
case '*':
|
||||
stack[len(stack)-1] *= num
|
||||
default:
|
||||
stack[len(stack)-1] /= num
|
||||
}
|
||||
preSign = ch
|
||||
num = 0
|
||||
}
|
||||
}
|
||||
for _, v := range stack {
|
||||
res += v
|
||||
}
|
||||
return res
|
||||
}
|
Reference in New Issue
Block a user