LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接: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的更多相关文章
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Leetcode#150 Evaluate Reverse Polish Notation
原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- scrapy常用命令
终端命令 创建一个项目: scrapy startproject name 利用蜘蛛名创建一个py文件: scrapy genspider name domain.name 在终端运行:scrapy ...
- [LeetCode&Python] Problem 762. Prime Number of Set Bits in Binary Representation
Given two integers L and R, find the count of numbers in the range [L, R](inclusive) having a prime ...
- JAVA 第四章 数组
数组保存的是一组有顺序的.具有相同类型的数据. 1.创建: 数组的声明格式: int arrary[]; int [] array1, array2; //同时声明多个数组. 上面的语句只是对数组进行 ...
- Gym.101908 Brazil Subregional Programming Contest(寒假自训第六场)
这几天睡眠时间都不太够,室友晚上太会折腾了,感觉有点累,所以昨天的题解也没写,看晚上能不能补起来. B . Marbles 题意:给定N组数(xi,yi),玩家轮流操作,每次玩家可以选择其中一组对其操 ...
- MyBatis #{} 取值注意事项
正确写法#{key} 错误写法#{key } #{}中不能加空格,不然会报错
- 【HDOJ1384】【差分约束+SPFA】
http://acm.hdu.edu.cn/showproblem.php?pid=1384 Intervals Time Limit: 10000/5000 MS (Java/Others) ...
- hibernate--DetachedCriteria(离线条件查询)
一.叙述 离线条件查询的好处,可以在非dao层封装查询参数,封装完成后,将对象传递到dao层,关联到session后,再去查询数据,这样做dao层可以极大的简化代码.下面通过一个小案例,一起来感受一下 ...
- Myelipse中xml约束文件的导入(以spring为例)
为了在电脑处于未联网状态下,beans.xml中书写标签具有提示功能,需要在电脑本地导入约束文件,下面上图 注意:将location后缀添加到key中beans的后面 注意:导入 context,ao ...
- itcast-svn
svn介绍 1.1 svn服务器的工作方式 数据 库 服务 概念 使用数据库,连接服务,服务操作库 独立服务器方式: svnserve 借助Apache方式: mod_dav_svn ...
- Object 及toString() 方法的重写
Object: 是所有的类的父类 ,Object中所有的方法 , 子类都能使用 , 接口不是Object子类. Person: /*将父类的equals方法 重写 * 不改变父类的源代码 eq ...