一.题目链接: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. switch语句和switch-case与if-else之间的转换

    switch语句格式:switch(变量){case 常量1:语句1;break;case 常量2:语句2;break;......default:语句;break;}特点:1.根据变量的值,选择相应 ...

  2. java知识整理

    整理一下Java知识点. 一.final finally finalize区别 1.final 修饰符(关键字).被final修饰的类,不能再派生出新的子类,不能作为父类而被子类继承.因此一个类不能既 ...

  3. Java中动态获取项目根目录的绝对路径

    https://www.cnblogs.com/zhouqing/archive/2012/11/10/2757774.html 序言 在开发过程中经常会用到读写文件,其中就必然涉及路径问题.使用固定 ...

  4. mac电脑读写NTFS格式的移动硬盘命令

    diskutil info /Volumes/SAMSUNG | grep UUID echo "UUID=38EBE5E4-016F-44B7-9D55-BB4AF6DC3E1D none ...

  5. Android 播放内部mp3音乐

    private void heartSound() { try { AssetManager am = getAssets();//获得该应用的AssetManager AssetFileDescri ...

  6. linux http配置

    yum install httpd 安装http服务器 启动http服务器即可访问 如果不行的话,试着执行命令 firewall-cmd –permanent –add-service=http(该命 ...

  7. UWP --- Display Content 显示基础内容

    UWP前端使用的是XAML语言, 这门语言和Xamarin Forms 是比较类似(当你开发多了之后会发现StackPanel和StackLayout傻傻的分不清) 言归正传. UWP中显示内容最简单 ...

  8. Primitives vs Objects

    这里首先我们要了解什么是primitives 和 objects 其实理解起来很简单. 如果我们懂.NET开发就会知道C#中的值类型和引用类型. primitives variables contai ...

  9. Python __name__变量

    原文: http://blog.csdn.net/u011511601/article/details/53504355 Python使用缩进对齐组织代码的执行,所有没有缩进的代码,都会在载入时自动执 ...

  10. AI update

    1, labeling工具 - 测试完成 使用fiji + Alps_Labeling_Tool.ijm 做labeling 生成的文件可以使用python读取 2,training -未开始 使用t ...