mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-25 12:14:26 +08:00
30 lines
575 B
Go
30 lines
575 B
Go
package leetcode
|
|
|
|
import (
|
|
"strconv"
|
|
)
|
|
|
|
func evalRPN(tokens []string) int {
|
|
stack := make([]int, 0, len(tokens))
|
|
for _, token := range tokens {
|
|
v, err := strconv.Atoi(token)
|
|
if err == nil {
|
|
stack = append(stack, v)
|
|
} else {
|
|
num1, num2 := stack[len(stack)-2], stack[len(stack)-1]
|
|
stack = stack[:len(stack)-2]
|
|
switch token {
|
|
case "+":
|
|
stack = append(stack, num1+num2)
|
|
case "-":
|
|
stack = append(stack, num1-num2)
|
|
case "*":
|
|
stack = append(stack, num1*num2)
|
|
case "/":
|
|
stack = append(stack, num1/num2)
|
|
}
|
|
}
|
|
}
|
|
return stack[0]
|
|
}
|