题目描述

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. EL条件判断用法<c:choose>

    EL表达式一般不直接用==,!=,>,<,>=,<=之类的表示相等.不等于.大于.小于.大于等于以及小于等于,而是使用字母表示,如下: ==     eq   等于 !=   ...

  2. 『Python』socket网络编程

    Python3网络编程 '''无论是str2bytes或者是bytes2str其编码方式都是utf-8 str( ,encoding='utf-8') bytes( ,encoding='utf-8' ...

  3. MongoDB 教程(八):查询文档、条件操作符

    MongoDB 查询文档 MongoDB 查询文档使用 find() 方法. find() 方法以非结构化的方式来显示所有文档. MongoDB 查询数据的语法格式如下: db.collection. ...

  4. Win10系列:C#应用控件基础16

    ToolTip控件 ToolTip控件常作为一些控件的子元素,当鼠标移动到指定控件上时在界面上弹出一个信息提示框.例如,为了节省窗体上的空间或增加美观性,仅在按钮上显示一个指示性图案,当鼠标移动到按钮 ...

  5. day35-python 操作memcache二

    Memcache常用命令 存储命令: set/add/replace/append/prepend/cas 获取命令: get/gets 其他命令: delete/stats.. add方法 添加一条 ...

  6. hadoop fsck详解

    我们知道fsck是用来检测hdfs上文件.block信息的,但是fsck输出的结果我们是否能看明白呢?   下面我们来看一个fsck输出的结果 hadoop fsck / ############## ...

  7. python高级变量类型(元组,列表,字典, 字符串和重要方法)

    高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...

  8. Spring手动提交事务

    // name的值根据spring配置文件的事物管理器的id而定 @Resource(name="transactionManager") private DataSourceTr ...

  9. git教程: 创建版本库

    转载:创建版本库 什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻 ...

  10. CSS精简工具——除去多余的css样式

    有时候开发网页中在改版之后,存在很多无意义的样式,对于后期的管理和维护很不友好. 如果手动去删除,很可能会导致出现更混乱的问题. 最近找到一个Chrome插件,CSS remove and combi ...