一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/

二.题目大意:

  给定后缀表达式,求出该表达式的计算结果。

三.题解:

  对于这道题目,首先观察后缀表达式(逆波兰表达式)的特点,那就是运算符在操作数的后面,所以每遇到一个运算符,只需找到它之前的最近的两个数字作为操作数,然后求出的结果作为一个新的操作数。很显然,可以使用一个栈来存储操作数,每遇到运算符从栈中取出顶端的两个数运算即可,运算结果再入栈。代码如下:

class Solution {
public:
int evalRPN(vector<string>& tokens) {
if(tokens.empty())
return 0;
stack<int> s;
for(int i = 0; i < tokens.size(); i++)
{
if(tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/")
{
int a = s.top();
s.pop();
int b = s.top();
s.pop();
if(tokens[i] == "+")
s.push(b + a);
if(tokens[i] == "-")
s.push(b - a);
if(tokens[i] == "*")
s.push(b * a);
if(tokens[i] == "/")
s.push(b / a);
}
else
s.push(stoi(tokens[i]));
}
return s.top();
}
};

该算法的时间复杂度为O(n);但此处有几点需要注意:

1.该算法的实现并不难,在string转换成int型时,此处利用了stoi()函数,stoi函数能够将string转换成int数字,它与atoi()作用类似,它们有以下几点不同:

(1)stoi()函数的形参是string型,而atoi()函数的形参是char *。

(2)stoi函数默认要求输入的参数字符串是符合int范围的[-2147483648, 2147483647],否则会runtime error;而atoi函数则不做范围检查,若超过int范围,则显示-2147483648(溢出下界)或者2147483647(溢出上界)。

(3)stoi头文件:<string>,c++函数;而atoi头文件:<cstdlib>,c函数

(我之前是自己写的转换函数,所以代码看起来比较乱,没有上传。。。)

2.string不同于char *,它可以直接用“==”进行比较,例如,string a; if(a == "123") ....;

3.莫忘记所写程序应考虑的三点:边界条件、特殊输入、错误处理。(《剑指offer》 P14)

LeetCode——150. Evaluate Reverse Polish Notation的更多相关文章

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

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

  2. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

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

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

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

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

  5. Leetcode#150 Evaluate Reverse Polish Notation

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

  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. 【LeetCode】150. Evaluate Reverse Polish Notation

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

  9. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

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

随机推荐

  1. Sublime 修改快捷键

    Preferences-Key Bindings-User 添加一行: { "keys": ["ctrl+d"], "command": & ...

  2. 阿里云ECS服务器购买流程 (自定义配置购买、按月、按量购买)教程

    阿里ECS云服务器自定义购买流程 本文提供全图文流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

  3. Gym.101955: Asia Shenyang Regional Contest(寒假自训第10场)

    C.Insertion Sort 题意:Q次询问,每次给出N,M,Mod,问你有多少种排列,满足前面M个数字排序之后整个序列的LIS>=N-1. 思路:我们把数字看成[1,M],[N-M+1,N ...

  4. 51Nod 1240:莫比乌斯函数

    1240 莫比乌斯函数  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使 ...

  5. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughter of a missing adv ...

  6. python 面向对象(类的成员,属性,绑定和非绑定,)

    面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实 ...

  7. 数字int字符串str(深入学习)

    数字int字符串str 查看一个对象的类 如:如查看对象变量a是什么类          用到函数type(),函数值是要查看的对象变量 1 #!/usr/bin/env python 2 # -*- ...

  8. ArrayList 与 LinkedList的区别

    今天查看源码,分析一下两者的异同.外加阐述内部类. 内部类参考官方文档,地址如下: https://docs.oracle.com/javase/tutorial/java/javaOO/nested ...

  9. 【java规则引擎】《Drools7.0.0.Final规则引擎教程》第4章 4.4 约束(Pattern的一部分)

    4.4.3 约束(Pattern的一部分) 前面我们已经介绍了条件约束在Pattern中位置了,那么什么是条件约束呢?简单来说就是一个返回true或者false的表达式,比如下面的5小于6,就是一个约 ...

  10. vue全家桶+Koa2开发笔记(5)--nuxt

    1. nuxt项目初始化报错 下面是使用 koa 模板方法初始化一个项目,使用该方法需要将 nuxt 的版本降至1.4.2: 官方 https://zh.nuxtjs.org/guide/instal ...