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 ...
随机推荐
- [LeetCode&Python] Problem 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- Tomcat介绍、安装jdk、安装Tomcat、配置Tomcat监听80端口
1.Tomcat介绍 2.安装jdk下载:wget -c http://download.oracle.com/otn-pub/java/jdk/10.0.1+10/fb4372174a714e6b8 ...
- 启动tomcat报错:Failed to start component [StandardEngine[Catalina].StandardHost[localhost]
1.右键点击需要启动的tomcat,选择Clean和Clean Tomcat Work Directory,清除即可!
- hdu3294 Girls' research manacher
One day, sailormoon girls are so delighted that they intend to research about palindromic strings. O ...
- day 51 html 学习 js 学习
函数 函数定义 JavaScript中的函数和Python中的非常类似,只是定义方式有点区别 // 普通函数定义 function f1() { console.log("Hello wor ...
- vorpal 又一个方便的cli 开发包
vorpal 是一个npm 包,我们可以用来开发专业的cli 程序 简单使用 初始化项目 yarn init -y 添加依赖 yarn add vorpal 简单demo app.js // cons ...
- Hasura GraphQL schema 生成是如何工作的
不像大部分的graphql 引擎,使用标准的graphql 规范的处理模型,Hasura graphql 不存在resolver 的概念(实际上是有的,只是转换为了sql语法) 以下是Hasura g ...
- Unity Blog 学习
The Profiler window https://unity3d.com/cn/learn/tutorials/temas/performance-optimization/profiler-w ...
- nginx配置.htaccess伪静态
https://blog.csdn.net/moqiang02/article/details/37695775
- Web-Business-Application-Solution
项目地址 : https://github.com/kelin-xycs/Web-Business-Application-Solution Web-Business-Application-Sol ...