题目描述

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. 微信小程序Promise对象

    Promise 对象 Promise 的含义 基本用法 Promise.prototype.then() Promise.prototype.catch() Promise.prototype.fin ...

  2. vue中修改了数据但视图无法更新的情况

    数组数据变动:我们使用某些方法操作数组,变动数据时,有些方法无法被vue监测,有些可以 Vue包装了数个数组操作函数,使用这些方法操作的数组去,其数据变动时会被vue监测: push() pop() ...

  3. Guava:好用的java类库 学习小记

    基础功能 google guava中定义的String操作 在google guava中为字符串操作提供了很大的便利,有老牌的判断字符串是否为空字符串或者为null,用指定字符填充字符串,以及拆分合并 ...

  4. scp ssh-key连接原理

    scp ssh-key连接原理   如何关闭位置解析 vim手动打开ssh的配置文件修改81行和122行,位置解析需要双方都关闭      注意重点部分   ssh特别提醒   22端口信息   连接 ...

  5. 如何破解MyEclipse 10.x

    本文以MyEclipse Professional 10.6 为例来介绍如何破解MyEclipse 10.x. 本文使用的破解补丁对MyEclipse Standard/ Professional/ ...

  6. tensoFlow之DNN文本分类

    TensorFlow文本分类: 亲测可用:https://blog.csdn.net/u012052268/article/details/77862202 简单实例:https://www.leip ...

  7. vs11 微软下载地址

    https://www.microsoft.com/en-us/download/details.aspx?id=30679 vs11 微软的官方下载.最安全

  8. 马凯军201771010116《面向对象程序设计(java)》第七周学习总结

    理论与知识部分 多态性:概念:指在程序中同一符号在不同的情况下具有不同的解释.超类中定义的域或方法,被子类继承之后,可以具有不同的数据类型或表现出不同的行为.这使得同一域或方法在超类及各个子类中具有不 ...

  9. selenium+grid做分布式测试

    一.grid介绍 1.本文用的是selenium-server-standalone-3.8.1.jar 2.Firefox用的55版本和对应的驱动 二.grid使用流程说明比如有个A机器,作用是hu ...

  10. ASP.NET中出现内存溢出错误System.OutOfMemoryException

    原因1:数据库服务器性能问题导致内存不够用,从而引起内存溢出 原因2:在IIS的应用程序池中进行配置,引起IIS服务器的内存分配问题,从而引起内存溢出   分析:      32位操作系统的寻址空间是 ...