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]
}
相关推荐
2501_941982057 小时前
深度对比:Java、Go、Python 实现企微外部群推送,哪个效率更高?
java·golang·企业微信
alphaTao8 小时前
LeetCode 每日一题 2026/2/2-2026/2/8
算法·leetcode
甄心爱学习8 小时前
【leetcode】判断平衡二叉树
python·算法·leetcode
不知名XL9 小时前
day50 单调栈
数据结构·算法·leetcode
@––––––9 小时前
力扣hot100—系列2-多维动态规划
算法·leetcode·动态规划
YuTaoShao10 小时前
【LeetCode 每日一题】1653. 使字符串平衡的最少删除次数——(解法三)DP 空间优化
算法·leetcode·职场和发展
cpp_250110 小时前
P10570 [JRKSJ R8] 网球
数据结构·c++·算法·题解
cpp_250111 小时前
P8377 [PFOI Round1] 暴龙的火锅
数据结构·c++·算法·题解·洛谷
TracyCoder12311 小时前
LeetCode Hot100(26/100)——24. 两两交换链表中的节点
leetcode·链表
cpp_250111 小时前
P9586 「MXOI Round 2」游戏
数据结构·c++·算法·题解·洛谷