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]
}
相关推荐
Dream it possible!3 小时前
LeetCode 热题 100_只出现一次的数字(96_136_简单_C++)(哈希表;哈希集合;排序+遍历;位运算)
c++·leetcode·位运算·哈希表·哈希集合
每天一个秃顶小技巧6 小时前
02.Golang 切片(slice)源码分析(一、定义与基础操作实现)
开发语言·后端·python·golang
恋喵大鲤鱼6 小时前
Golang 空结构体特性与用法
golang·空结构体
MarkHard1238 小时前
Leetcode (力扣)做题记录 hot100(34,215,912,121)
算法·leetcode·职场和发展
Kidddddult9 小时前
力扣刷题Day 46:搜索二维矩阵 II(240)
算法·leetcode·力扣
q567315239 小时前
Go语言多线程爬虫与代理IP反爬
开发语言·爬虫·tcp/ip·golang
Chandler249 小时前
Go语言即时通讯系统 开发日志day1
开发语言·后端·golang
是代码侠呀13 小时前
从前端视角看网络协议的演进
leetcode·开源·github·github star·github 加星
李匠202415 小时前
C++GO语言微服务基础技术②
开发语言·c++·微服务·golang
BUG制造机.15 小时前
Go 语言 slice(切片) 的使用
开发语言·后端·golang