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 {
public:
int evalRPN(vector<string> &tokens) {
stack<int> s;
for(int i=;i<tokens.size();i++){
string cur = tokens[i];
int temp = atoi(cur.c_str());
if(cur=="+"||cur=="-"||cur=="*"||cur=="/"){
int left,right;
if(!s.empty()){
right=s.top();
s.pop();
}
if(!s.empty()){
left=s.top();
s.pop();
}
if(cur=="+")
temp=left+right;
else if(cur=="-")
temp=left-right;
else if(cur=="*")
temp=left*right;
else if(cur=="/")
temp=left/right; }
s.push(temp);
}
int res = s.top();
return res;
}
};

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

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

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

  4. leetcode - [2]Evaluate Reverse Polish Notation

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

  5. 【LeetCode】150. Evaluate Reverse Polish Notation

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

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

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

  7. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

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

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

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

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

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

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

随机推荐

  1. 数据结构( Pyhon 语言描述 ) — —第10章:树

    树的概览 树是层级式的集合 树中最顶端的节点叫做根 个或多个后继(子节点). 没有子节点的节点叫做叶子节点 拥有子节点的节点叫做内部节点 ,其子节点位于层级1,依次类推.一个空树的层级为 -1 树的术 ...

  2. pytorch 加载数据集

    pytorch初学者,想加载自己的数据,了解了一下数据类型.维度等信息,方便以后加载其他数据. 1 torchvision.transforms实现数据预处理 transforms.Totensor( ...

  3. 与LCD_BPP相关的函数

    board/freescale/mx6q_sabresd/mx6q_sabresd.c:    panel_info.vl_bpix = LCD_BPP; common/lcd.c:   off  = ...

  4. 记如何解决蓝桥杯中to_string和atoi等无法使用的问题

    #include<iostream> #include<sstream> using namespace std; int main() {   // int 转 string ...

  5. ACM训练联盟周赛 Teemo's formula

    Teemo has a formula and he want to calculate it quickly. The formula is . As the result may be very ...

  6. LeetCode01--回文数

    ''' 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: ...

  7. 降维算法-PCA主成分分析

    1.PCA算法介绍主成分分析(Principal Components Analysis),简称PCA,是一种数据降维技术,用于数据预处理.一般我们获取的原始数据维度都很高,比如1000个特征,在这1 ...

  8. Idea使用Tomcat乱码 tomcat 9.0 8.5.37乱码

    使用新版tomcat 如8.5.37,9.0.14的时候idea控制台输出乱码,很简单老版本的如8.5.31就不会乱码,使用比较工具比较一下发现如下变化, 关键的关键是\apache-tomcat-8 ...

  9. Java单例模式简单实现

    代码 public class Singleton { private static Singleton singleton;//创建一个单例对象 public static Singleton ge ...

  10. FFT-hdu题目练习

    网上FFT的讲解和板子有很多,所以直接放题目 hdu1402 http://acm.hdu.edu.cn/showproblem.php?pid=1402 /* problem:大整数乘法 solut ...