150. Evaluate Reverse Polish Notation
来源:互联网 发布:淘宝口红代购知乎 编辑:程序博客网 时间:2024/06/15 10:17
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
class Solution { public int evalRPN(String[] tokens) { Stack<Integer> s = new Stack<Integer>(); String operators = "+-*/"; for(String token : tokens){ if(!operators.contains(token)){ s.push(Integer.valueOf(token)); continue; } int a = s.pop(); int b = s.pop(); if(token.equals("+")) { s.push(b + a); } else if(token.equals("-")) { s.push(b - a); } else if(token.equals("*")) { s.push(b * a); } else { s.push(b / a); } } return s.pop(); }}
阅读全文
0 0
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- Java:如何用链表实现队列
- erlang shell 命令中文使用说明
- HTML(五) 使用img标签
- Air800(GPRS+GPS模块)连接OneNet平台
- LeetCode
- 150. Evaluate Reverse Polish Notation
- ElasticSearch 常用的查询过滤语句
- 产品经理如何学机器学习——一篇以产品为中心的机器学习概论
- 专访 | 在AI 医疗这个热门的赛道上,阿里在怎么玩?
- Flask-Moment本地化日期和时间显示不正常
- CentOS下通过yum安装svn及配置
- lua脚本操作redis数据库
- 这些科技公司们的名字,都是从何而来?
- 洛谷P2055 [ZJOI2009]假期的宿舍(二分图,匈牙利算法)