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. python3 迭代器&生成器

    前戏:列表生成式 等于 用列表生成式生成列表.需要将所有数据生成到内存中,占用空间,如果数据太多.生成数据就会耗时较久. 例如需要运行卡顿一下..... 定义一个生成器:定义时不生成任何数据,只有通过 ...

  2. SpringBoot简单整合Actuator监控

    pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>s ...

  3. 使用docker自定义oraclejdk启动jar包

    Dockerfile文件 FROM centos:7 #把java与tomcat添加到容器中 ADD jdk-8u161-linux-x64.tar.gz /usr/local/ #安装 vim编辑器 ...

  4. 使用docker创建含有FFmpeg的自定义镜像

    Dockerfile文件 FROM openjdk:8-jre-alpine MAINTAINER "yvioo" RUN echo "http://mirrors.al ...

  5. C/C++ 基本类型 占字节

    下面给出不同位数编译器下的基本数据类型所占的字节数: 16位编译器 char :1个字节char*: 2个字节(即指针变量)short: 2个字节int:  2个字节unsigned int : 2个 ...

  6. mac osx 准备 nanogui 记录

    !!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist mac osx : 10.14.6 Mojave. 之前没有配置openGL相关开发环境,自 ...

  7. c++关于使用new的纠正

    自己之前纠正过这个问题,但还是忘了.今天再拿出来. 今天主要总结关于使用 c++ 标准中的 new 关键字. [结论] A.处理new可能抛出的异常 B.针对new使用std::nothrow不抛出异 ...

  8. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  9. 【LeetCode】398. Random Pick Index 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...

  10. C# RabbitMQ的使用

    本文目的如题. 安装 先说一下RabbitMQ的安装,建议使用Docker镜像安装,Docker安装的好处是不管Windows系统还是Linux,安装步骤少,安装方法相同,不容易出错.使用下面的命令就 ...