1. Evaluate Reverse Polish Notation

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

Valid operators are +, -, *, /. Each operand may be an integer or another expression.

Note:

  • Division between two integers should truncate toward zero.
  • The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.

Example 1:

Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Example 3:

Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Explanation:
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

逆波兰表达式中,两个操作数紧跟一个运算符

class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int>s1;
for(int i = 0; i < tokens.size(); ++i){
if(tokens[i].size() == 1 && !isdigit(tokens[i][0])){
int x1 = s1.top();
s1.pop();
int x2 = s1.top();
s1.pop();
s1.push(compute(x2, x1, tokens[i]));
}else{
s1.push(stoi(tokens[i]));
}
}
return s1.top();
}
int compute(int &x1, int &x2, string &op){
if(op == "+"){
return x1 + x2;
}else if(op == "-"){
return x1 - x2;
}else if(op == "*"){
return x1 * x2;
}else if(op == "/"){
return x1 / x2;
}
return -1;
}
};

Note : 影响代码速度

  1. for循环中直接枚举会慢一些
  2. 函数传参时,传值会慢一些,尽量使用传引用

【刷题-LeetCode】150 Evaluate Reverse Polish Notation的更多相关文章

  1. 【leetcode刷题笔记】Evaluate Reverse Polish Notation

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

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

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

  3. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

  4. leetcode 150. Evaluate Reverse Polish Notation ------ java

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

  5. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

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

  6. Leetcode#150 Evaluate Reverse Polish Notation

    原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...

  7. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

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

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

  9. 150. Evaluate Reverse Polish Notation - LeetCode

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

随机推荐

  1. idea删除同一个模块后新建模块显示被占用

    当我们某个模块因为什么原因需要删除重建的时候 ,输入完模块名称并不能创建出来,这是因为模块已经被注册 解决办法: 1.右键点击项目名称---选择Load/Unload Modules 2.将已经删除的 ...

  2. win10 1909+ vs2015up3 使用fmt概述(fmt version 7.0.1)

    !!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist fmt 源码: https://github.com/fmtlib/fmt fmt官方文档: ...

  3. 【九度OJ】题目1107:搬水果 解题报告

    [九度OJ]题目1107:搬水果 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1107 题目描述: 在一个果园里,小明已经将所有的水 ...

  4. 【LeetCode】794. Valid Tic-Tac-Toe State 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/valid-ti ...

  5. 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...

  6. C. Tourist's Notes

    C. Tourist's Notes time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. 深入理解Java虚拟机一:运行时数据区域

    根据<Java虚拟机规范(第2版)>的规定,Java虚拟机管理的内存包括下图几个运行时数据区域: 1.程序计数器        程序计数器(Program Counter Register ...

  8. OverFeat:Integrated Recognition, Localization and Detection using Convolutional Networks

    目录 概 主要内容 Sermanet P., Eigen D., Zhang X., Mathieu M., Fergus R., LeCun Y. OverFeat:integrated recog ...

  9. BAIRE ONE FUNCTIONS (Baire第一类函数)

    目录 定义 导函数 一致收敛性质 的连续点 JOHNNY HU, BAIRE ONE FUNCTIONS. 一些基本的定义(诸如逐点收敛, 一致收敛\(F_{\sigma}\)集合等)就不叙述了. 定 ...

  10. DEEP DOUBLE DESCENT: WHERE BIGGER MODELS AND MORE DATA HURT

    目录 概 主要内容 Effective Model Complexity(EMC) label noise data augmentation 下降方式 SGD vs Adam Adam SGD SG ...