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 ...
随机推荐
- CENTOS6 安装配置 pptpd 心得
1.你所需要的软件 pppd ppp拨号服务器pptpd 在pppd拨号的基础上增加pptpd的支持 2.确定你的内核是否支持mppe modprobe ppp-compress-18 &a ...
- Linux创建LVM
###########format disk############ 格式化磁盘,将其SystemId修改为8e fdisk /dev/sdb n p 1 [enter] [enter] t 8e w ...
- 伪命题:PHP识别url重写请求
手上有一个网站,然后启用了伪静态,因为一些设置上的原因,一段时间后,发现收录的都是.php的文件,而启用的伪静态地址则收录很少,在更改设置后,想尽快去掉.php的收录,然后想将.php的地址转向.ht ...
- Flume Hello World!
Flume 是 Cloudera 公司开源出来的一套日志收集系统.模型如下所示: 图中Source,Sink分别代表数据源和数据目的地,channel表示Source和Sink之间的通道.配置文件为/ ...
- java成员变量与局部变量修饰符的区别
成员变量: 可以被 public,static ,protected,default,final修饰. 局部变量:包括方法里的和 代码块里的(静态和非静态) 可以被default, final修饰 参 ...
- document.cookie的使用
设置cookie每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie:document.cookie="userId=828";如果要一次 ...
- OL/SQL编程练习
create or replace procedure pr_first is --一个变量 v_a ) := '总有一天我的生命将走到尽头'; --一个常量 c_b constant ) := '而 ...
- 用时间生成用户Id
用用户注册时的时间,作为新用户的Uid: /** * 生成用户id,用时间生成 * * @return */ public static String date2UserId() { SimpleDa ...
- 每天学一点JAVA
1.JAVA的反射机制 在运行时判断任意一个对象所属的类:在运行时构造任意一个类的对象:在运行时判断任意一个类所具有的成员变量和方法:在运行时调用任意一个对象的方法:生成动态代理. 2.关于ARRAY ...
- C++指针详解(二)
指针是C/C++编程中的重要概念之一,也是最容易产生困惑并导致程序出错的问题之一.利用指针编程可以表示各种数据结构,通过指针可使用主调函数和被调函数之间共享变量或数据结构,便于实现双向数据通讯:指针能 ...