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的更多相关文章

  1. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  2. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  3. 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 ...

  4. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  5. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  6. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  7. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  8. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

  9. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  10. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

随机推荐

  1. 如何删除 Ubuntu 上不再使用的旧内核

    提问:过去我已经在我的Ubuntu上升级了几次内核.现在我想要删除这些旧的内核镜像来节省我的磁盘空间.如何用最简单的方法删除Ubuntu上先前版本的内核? 在Ubuntu上,有几个方法来升级内核.在U ...

  2. 菜鸟开始学习SSDT HOOK((附带源码)

    看了梦无极的ssdt_hook教程,虽然大牛讲得很细,但是很多细节还是要自己去体会,才会更加深入.在这里我总结一下我的分析过程,若有不对的地方,希望大家指出来.首先我们应该认识 ssdt是什么?从梦无 ...

  3. Android Webview 背景透明

    两个关键点: 1         fBarParams.format = PixelFormat.RGBA_8888; 2 mWebView.setBackgroundColor(Color.TRAN ...

  4. 一模 (1) day1

    第一题:(水题) 题目大意:求出n个  X% (X是小于等于2位的整数) 的乘积,去掉末尾的0: 解题过程: 1.直接 把整数乘好,然后确定小数点的位置,去掉多余的0 输出即可. 第二题:(搜索题) ...

  5. C# Lodop实现打印

    项目的Debug文件夹下有个template文件夹,里面有用到的js.自己建的要打印的网页和用到的背景图 1.打印方法: class print { public void printzb(strin ...

  6. 迭代输出Map和List<Map<String,Object>>的方法

    一.Map<String,Object> String:key的类型 Object:value的类型,value可能是String,或者int类型,什么类型都可以 对于Map接口来说,本身 ...

  7. CRF条件随机场简介

    CRF(Conditional Random Field) 条件随机场是近几年自然语言处理领域常用的算法之一,常用于句法分析.命名实体识别.词性标注等.在我看来,CRF就像一个反向的隐马尔可夫模型(H ...

  8. AIX查看内存卡槽

    1.lscfg -vp|grep Processor 2.lscfg -vp|grep -p Memory

  9. hql抓取要注意的点

    fetchtype是lazy,那就用到了在通过缓存中的关联去取,用不到不取:lazy遇到joinfetch就失去意义,但是由于hql语句是自己编写的,可以控制加不加fetch 所以如果主力是hql语句 ...

  10. 12-27cell常用的属性

    1.创建cell //    创建一个cell并且设置cell的风格 UITableViewCell *cell  = [[UITableViewCell alloc]initWithStyle:UI ...