一.题目链接: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. 在 Mac 安装Docker

    https://blog.csdn.net/jpiverson/article/details/50685817 https://legacy.gitbook.com/book/yeasy/docke ...

  2. select标签(分组下拉菜单和列表)

    分组下拉菜单和列表标签: <select name=" " > <optgroup label="组1"> <option val ...

  3. HDU 1907:John(尼姆博弈变形)

    John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submi ...

  4. ajax及其工作原理

    1.关于ajax的名字 ajax 的全称是Asynchronous JavaScript and XML,其中,Asynchronous 是异步的意思,它有别于传统web开发中采用的同步的方式. 2. ...

  5. java知识 特殊符号转换

    ■情况 想把代码中的出现  “  ’等特殊符号时,在他们的前面,转换时自动加 \    最后转换成json 决定用ObjectMapper这个类,先准备一个Map,之后,map作为一个参数,调用Obj ...

  6. putty登陆sourceforge.net(设置登录)

    打开putty.exe session选项的host name (ip address) 填写 shell.soureceforge.net 端口22(不变) 接下来是connection选项子目录下 ...

  7. 每天进步一点点-写完睡觉-周一工作(java基本数据类型所占的字节和IO流读取的字符和字节)

  8. golang json html escape unicode

    https://play.golang.org/p/FAH-XS-QMC https://github.com/gin-gonic/gin/issues/693 package main import ...

  9. Djangon 基础总结 汇总 从请求到返回页面的过程,

    第一步我是用户 现在 浏览器上输入地址 ---> 发送给服务   来请求返回当前的页面 第二步  服务端获得我当前的客户端要求访问的地址   第三步 服务端去urls.py中去,来看是要访问那个 ...

  10. Gravitational Teleport简单使用

    使用官方提供的二进制包进行快速启动测试,详细细节还需要在学习 下载软件包 mac 系统 https://gravitational.com/teleport/download/ wget https: ...