MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / evalRPN

Function evalRPN

go/0150-evaluate-reverse-polish-notation.go:1–24  ·  view source on GitHub ↗
(tokens []string)

Source from the content-addressed store, hash-verified

1func evalRPN(tokens []string) int {
2 var stack []int
3 var a, b int
4 for _, c := range tokens {
5 switch c {
6 case "+":
7 a, b, stack = getAndPopLastOperand(stack)
8 stack = append(stack, (a + b))
9 case "-":
10 a, b, stack = getAndPopLastOperand(stack)
11 stack = append(stack, (a - b))
12 case "*":
13 a, b, stack = getAndPopLastOperand(stack)
14 stack = append(stack, (a * b))
15 case "/":
16 a, b, stack = getAndPopLastOperand(stack)
17 stack = append(stack, (a / b))
18 default:
19 i, _ := strconv.Atoi(c)
20 stack = append(stack, i)
21 }
22 }
23 return stack[0]
24}
25
26func getAndPopLastOperand(stack []int) (int, int, []int) {
27 a := stack[len(stack)-2]

Callers

nothing calls this directly

Calls 1

getAndPopLastOperandFunction · 0.85

Tested by

no test coverage detected