Evaluate Reverse Polish Notation
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{
private:
bool isOperator(string s){
if(s.length()== && string("+-*/").find(s) != string::npos)
return true;
else
return false;
}
public:
int evalRPN(vector<string>& tokens){
stack<int> s;
for (string token : tokens){
if(!isOperator(token)){
s.push(stoi(token));
}else{
int y = s.top();
s.pop();
int x = s.top();
s.pop();
if(token == "+"){
s.push(x+y);
}else if(token == "-"){
s.push(x-y);
}else if(token == "*"){
s.push(x*y);
}else if(token == "/"){
s.push(x/y);
}
}
}
return s.top();
}
};
Evaluate Reverse Polish Notation的更多相关文章
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- linux 安装 apache
1. 系统基本信息 CentOS 6.4 内存2G 硬盘 200G cpu 4核 (cat /proc/cpuinfo |grep 'processor'|wc -l 查看cpu核数 ...
- eclipse 连接 mysql
1.下载驱动. 2.eclipse->add extend jars -> 添加驱动. 3.测试: 在mysql 建立数据库和表,在eclipse 里对数据库进行操作. 代码: mysql ...
- POJ 3249 拓扑排序+DP
貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...
- 使用NuGet时的一个乌龙
问题描述 最近自己做的一个项目,计划开始使用NuGet来管理dll,但是遇到一个奇怪,但是结果证明是个乌龙的问题. 新建一个WebApi项目,使用NuGet管理第三方dll,其中有引用Newtonso ...
- IT公司100题-21-输入n和m,和等于m
问题描述: 输入两个整数n 和m,从数列1,2,3,…,n 中随意取几个数, 使其和等于m,将所有可能的组合都打印出来. 分析: 利用递归的思路,对于1,2,3,…,n 中的任意一个数,要么选,要 ...
- redis学习(一)
一.redis简介 Redis是基于内存.可持久化的日志型.key-value高性能存储系统.关键字(Keys)是用来标识数据块.值(Values)是关联于关键字的实际值,可以是任何东西.有时候你会存 ...
- 银行支票和汇票中使用的专用字体MICR E13B条形码控件字体
MICR E13B条形码控件字体是一种在美国.加拿大.波多黎各.巴拿马.英国和其它少数国家的银行支票和汇票中使用的专用字体,主要用来打印适用于磁性和光学字符识别系统的MICR字符.MICR E13B条 ...
- Android获取手机设备识别码(IMEI)和手机号码
最近看了下获取手机设备ID和手机信息以及SIM的信息例子,主要还是借鉴别人的,现在自己写一下,算是巩固加深了,也希望能给大家一个参考 必要的条件还是一部真机,SIM卡或者UIM卡. 首先,在Andro ...
- Servlet三种实现方法(四)
开发Servlet有三种方式:1.实现Servlet接口2.通过继承GenericServlet3.通过继承HttpServlet 一.实现Servlet接口 需求如下:请使用实现 接口的方式,来开发 ...
- Android常见控件— — —TextView
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=&qu ...