当前位置:网站首页>golang刷leetcode 字符串(4)逆波兰式
golang刷leetcode 字符串(4)逆波兰式
2022-08-02 17:38:00 【用户9710217】
根据逆波兰表示法,求表达式的值。
有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
说明:
整数除法只保留整数部分。
给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。
示例 1:
输入: ["2", "1", "+", "3", "*"]
输出: 9
解释: ((2 + 1) * 3) = 9示例 2:
输入: ["4", "13", "5", "/", "+"]
输出: 6
解释: (4 + (13 / 5)) = 6示例 3:
输入: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
输出: 22
解释:
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22解题思路:
本题很简单,理解逆波兰式就ok
逆波兰式求解原理:
1,从左往右扫描token
2,如果式操作数,入栈
3,如果是操作符,弹出两个操作数
4,计算结果,将结果入栈
5,扫描完token,栈中,剩下结果,结果出栈
import "strconv"
func evalRPN(tokens []string) int {
var s stack
for i := 0; i < len(tokens); i++ {
if !isOperator(tokens[i]) {
s.Push(tokens[i])
} else {
op2 := s.Pop()
op1 := s.Pop()
res := eval(op1, op2, tokens[i])
s.Push(fmt.Sprint(res))
}
}
r := s.Pop()
rv, _ := strconv.ParseInt(r, 10, 64)
return int(rv)
}
func eval(op1 string, op2 string, token string) int64 {
o1, _ := strconv.ParseInt(op1, 10, 64)
o2, _ := strconv.ParseInt(op2, 10, 64)
switch token {
case "+":
return o1 + o2
case "-":
return o1 - o2
case "*":
return o1 * o2
case "/":
return o1 / o2
}
return 0
}
func isOperator(token string) bool {
return token == "+" || token == "-" || token == "*" || token == "/"
}
type stack struct {
data []string
}
func (s *stack) Push(str string) {
s.data = append(s.data, str)
}
func (s *stack) Pop() string {
if len(s.data) < 1 {
return ""
}
str := s.data[len(s.data)-1]
s.data = s.data[:len(s.data)-1]
return str
}边栏推荐
猜你喜欢

Smart Microelectronics Releases Low-Power MM32L0130 Series MCU Products

方法的使用

文件上传很难搞?10分钟带你学会阿里云OSS对象存储

NeRF: The Secret of 3D Reconstruction Technology in the Popular Scientific Research Circle

新特性解读 | MySQL 8.0 GIPK 不可见主键

一文看懂推荐系统:概要01:推荐系统的基本概念

Playing in the cloud | The key technology of Tianyi cloud object storage ZOS high availability is revealed

土巴兔IPO五次折戟,互联网家装未解“中介”之痛

Security First: Tools You Need to Know to Implement DevSecOps Best Practices

How Tencent architects explained: The principle of Redis high-performance communication (essential version)
随机推荐
golang刷leetcode 经典(3) 设计推特
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Works (7) Mid-term Inspection Report
vulnhub W34kn3ss: 1
Remember the stuck analysis of an industrial automation control system in .NET
小程序毕设作品之微信体育馆预约小程序毕业设计成品(6)开题答辩PPT
基于HDF的LED驱动程序开发(1)
SQL 正则解析手机号码提供商
Smart Microelectronics Releases Low-Power MM32L0130 Series MCU Products
redis总结_基础
字节面试官狂问我:你没有高并发、性能调优经验,为什么录取你?
C语言中的一系列操作符
KunlunBase 1.0 is released!
php弱类型-攻防世界lottery
2021年下半年软件设计师上午真题
CUDA+Pycharm-gpu版本+Anaconda安装
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (5) Task Book
一文看懂推荐系统:概要01:推荐系统的基本概念
来亲自手搭一个ResNet18网络
Mysql和Redis如何保证数据一致性
Taking advantage of cloud-network integration, e-Surfing Cloud has paved the way for digital transformation for government and enterprises