Golang | Leetcode Golang题解之第150题逆波兰表达式求值

题目:

题解:

Go 复制代码
func evalRPN(tokens []string) int {
    stack := make([]int, (len(tokens)+1)/2)
    index := -1
    for _, token := range tokens {
        val, err := strconv.Atoi(token)
        if err == nil {
            index++
            stack[index] = val
        } else {
            index--
            switch token {
            case "+":
                stack[index] += stack[index+1]
            case "-":
                stack[index] -= stack[index+1]
            case "*":
                stack[index] *= stack[index+1]
            default:
                stack[index] /= stack[index+1]
            }
        }
    }
    return stack[0]
}
相关推荐
柠石榴1 小时前
go-1 模型
开发语言·后端·golang
smj2302_796826526 小时前
解决leetcode第3753题范围内总波动值II
python·算法·leetcode
leoufung9 小时前
LeetCode 92 反转链表 II 全流程详解
算法·leetcode·链表
im_AMBER10 小时前
Leetcode 59 二分搜索
数据结构·笔记·学习·算法·leetcode
leoufung11 小时前
LeetCode 61. 旋转链表(Rotate List)题解与思路详解
leetcode·链表·list
想搞艺术的程序员11 小时前
深入 NSQ 延迟消息实现原理:设计巧思与性能优化
性能优化·golang·nsq
leoufung16 小时前
逆波兰表达式 LeetCode 题解及相关思路笔记
linux·笔记·leetcode
Aspect of twilight18 小时前
LeetCode华为大模型岗刷题
python·leetcode·华为·力扣·算法题
2301_8079973818 小时前
代码随想录-day47
数据结构·c++·算法·leetcode
Elias不吃糖18 小时前
LeetCode每日一练(3)
c++·算法·leetcode