题目描述

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

//判断字符串是否是数字,如果是就压栈,不是就弹出2个元素进行计算,将计算结果压栈

//最后栈顶元素即为所求
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> st;
for (int i = ; i < tokens.size(); i++){
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
int temp = ;
int a = st.top();
st.pop();
int b = st.top();
st.pop();
if (tokens[i] == "+") { temp = b + a; }
else if (tokens[i] == "-") { temp = b - a; }
else if (tokens[i] == "*") { temp = b * a; }
else { temp = b / a; }
st.push(temp);
}
else {
st.push(stoi(tokens[i]));
}
}
return st.top();
}
};

2、evaluate-reverse-polish-notation的更多相关文章

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

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

  2. 【leetcode】Evaluate Reverse Polish Notation

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

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

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

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

  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. Sumo生成数据

    1.生成input_net.net.xml文件 参数解释:http://www.sumo.dlr.de/userdoc/NETGENERATE.html#Grid_Network 1).生成grid ...

  2. 菜鸟webpack教程纠错

    gei事例: http://www.runoob.com/w3cnote/webpack-tutorial.html 本次的问题主要是在loader部分,原因是按照教程的操作,会出现一下错误 后来发现 ...

  3. MVC和MVVM对比

    被误解的 MVC MVC 的历史 MVC,全称是 Model View Controller,是模型 (model)-视图 (view)-控制器 (controller) 的缩写.它表示的是一种常见的 ...

  4. Dao层的sql语句

    2018-08-12     21:33:43 反思:在数据库执行的时候,sql语句是正确的,复制到方法中,执行出错   因为把限定条件改为?时,把左括号删掉了,sql语句报错 改正:一定要确保sql ...

  5. virsh命令和虚拟机克隆

    virsh 命令 virsh list  //列出正在运行虚拟机 virsh list --all     //列出所有虚拟机 virsh  console sunhao-1  //进入名字为sunh ...

  6. MFC VC++ 根据文件名获取程序的Pid

    环境:PC Win7 VS VC++ .MFC 使用,输入文件名即可获取程序的pid,进而可以对程序进行操作,比如关闭Porcess等. 头文件: #include <TlHelp32.h> ...

  7. Git 简介及简单操作

    一.GIT是什么 Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本 ...

  8. Python全栈之路----函数----参数

    参数可以让你的函数更灵活,不只能做死的动作,还可以根据调用时传参的不同决定函数内部的执行流程. 形参:只有在被调用时才分配内存单元,在调用结束时,即可释放所分配的内存单元.因此形参只在函数内部有效.函 ...

  9. ViewpageMaiActity

    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=&qu ...

  10. A New Function(LightOJ 1098)积性函数前缀和的应用

    题意:要求对于1~n,每个数的约数(不包括1和其本身)的和. 题解:由于题目数据有2*10^9之大,因而不能直接暴力.需要考虑积性函数的特性,由于必定有重复的约数出现,因而可以对重复约数所在的区间进行 ...