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]
}
相关推荐
平头哥AI9 小时前
Day 22 _ 包装错误别丢链_%w、errors.Is 与 errors.As
android·服务器·学习·golang·go
Navigator_Z13 小时前
LeetCode //C - 1240. Tiling a Rectangle with the Fewest Squares
c语言·算法·leetcode
稻米哟13 小时前
力扣100——双指针
算法·leetcode
golang学习记14 小时前
Go 1.27新特性: json/v2使用有趣指南
开发语言·golang·json
王码码203515 小时前
Go语言CGO:Go与C交互
后端·golang·go·接口
PC2005-cloud16 小时前
DSH 白嫖指南:接入 Command Code Go、WorkBuddy 与 Trae 的免费额度
开发语言·后端·golang
橘子汽水16817 小时前
Leetcode 198,118打家劫舍,杨辉三角
算法·leetcode
名字还没想好☜17 小时前
Docker 镜像瘦身进阶:用 distroless/scratch 把 Go 服务打到 10MB,以及没 shell 怎么调试
运维·docker·容器·golang·kubernetes
lvshuocool17 小时前
golang 多版本管理工具 -- g
开发语言·后端·golang
海盗123420 小时前
DSH 接入 OpenCode Go 报 400 MissingSessionID:从 LLM 语义层退到 fetch 传输层的完整排障
开发语言·后端·golang