Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Jan 30, 2024
1 parent 3ea7415 commit 06aed1e
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// https://leetcode.com/problems/evaluate-reverse-polish-notation

package main

import (
"fmt"
"strconv"
)

func evalRPN(tokens []string) int {
var stack []int
stackSize := 0

for _, token := range tokens {
if token == "+" || token == "-" || token == "*" || token == "/" {
var res int
op1, op2 := stack[stackSize-2], stack[stackSize-1]

switch token {
case "+":
res = op1 + op2
case "-":
res = op1 - op2
case "*":
res = op1 * op2
case "/":
res = op1 / op2
}

stack = stack[0 : stackSize-2]
stack = append(stack, res)

stackSize--

continue
}

converted, _ := strconv.Atoi(token)

stack = append(stack, converted)
stackSize++
}

return stack[0]
}

func main() {
tokens := []string{"2", "1", "+", "3", "*"}

fmt.Println(evalRPN(tokens))
}

0 comments on commit 06aed1e

Please sign in to comment.