150 evaluate reverse polish notation

·data-structure-and-algorithm
#stack-and-queue

150. 逆波兰表达式求值

go:

import "strconv"

func evalRPN(tokens []string) int {
    stack := make([]int, 0, len(tokens))

    for _, v := range tokens {
        switch v {
            case "+":
                a := stack[len(stack) - 1]
                b := stack[len(stack) - 2]
                stack = stack[: len(stack) - 2]
                stack = append(stack, a + b)
            case "-":
                a := stack[len(stack) - 1]
                b := stack[len(stack) - 2]
                stack = stack[: len(stack) - 2]
                stack = append(stack, b - a)
            case "*":
                a := stack[len(stack) - 1]
                b := stack[len(stack) - 2]
                stack = stack[: len(stack) - 2]
                stack = append(stack, a * b)
            case "/":
                a := stack[len(stack) - 1]
                b := stack[len(stack) - 2]
                stack = stack[: len(stack) - 2]
                stack = append(stack, b / a)
            default:
                num, err := strconv.Atoi(v)

                if err != nil {
                    panic("invalid token: " + v)
                }

                stack = append(stack, num)
        }
    }
    
    return stack[0]
}