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]
}
相关推荐
想搞艺术的程序员30 分钟前
Go Error 全方位解析:原理、实践、扩展与封装
开发语言·后端·golang
竹等寒30 分钟前
Go红队开发—图形化界面
网络安全·golang·个人开发
熬了夜的程序员1 小时前
【LeetCode】99. 恢复二叉搜索树
算法·leetcode·职场和发展
Kent_J_Truman2 小时前
LeetCode Hot100 自用
算法·leetcode·职场和发展
还是码字踏实2 小时前
算法题种类与解题思路全面指南:基于LeetCode Hot 100与牛客Top 101
算法·leetcode
熬了夜的程序员4 小时前
【LeetCode】101. 对称二叉树
算法·leetcode·链表·职场和发展·矩阵
Kuo-Teng5 小时前
LeetCode 73: Set Matrix Zeroes
java·算法·leetcode·职场和发展
葵续浅笑5 小时前
LeetCode - 杨辉三角 / 二叉树的最大深度
java·数据结构·算法·leetcode
Miraitowa_cheems6 小时前
LeetCode算法日记 - Day 94: 最长的斐波那契子序列的长度
java·数据结构·算法·leetcode·深度优先·动态规划