题目

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

Valid operators are +-*/.
Each operand may be an integer or another expression.

Some examples:

  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

原题地址

解题思路

计算逆波兰表达式。了解很多其它关于逆波兰表达式请点击

计算逆波兰表达式这是个非常典型的栈应用的样例。

解题方法就是用栈来处理。须要注意的是本题输入给的是字符串数组,所以须要在字符串和整数之间有个转换。

代码例如以下

class Solution {
public:
int evalRPN(vector<string> &tokens) {
int ret=0;
int n = tokens.size();
if(n<=0) return ret;
stack<int> s;
int i=0;
while(i<n){
string temp = tokens[i];
int num = atoi(temp.c_str());
//防止非法输入
if(num!=0 || (num==0 && temp[0]=='0')){
s.push(num);
}else{
ret = cal(s, tokens[i][0]);
if(ret == -1) return 0;
}
++i;
}
if(!s.empty()) return s.top();
else return 0;
}
int cal(stack<int> &s, char oper){
if(s.size()<2) return -1;
int op1 = s.top(); s.pop();
int op2 = s.top(); s.pop();
if(oper == '+'){
s.push(op1+op2);
}else if(oper == '-'){
s.push(op2-op1);
}else if(oper == '*'){
s.push(op2 * op1);
}else if(oper == '/'){
if(op1 == 0) return -1;
s.push(op2 / op1);
}else return -1;
return 0;
}
};

假设你认为本篇对你有收获。请帮顶。

另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.

你能够搜索公众号:swalge 或者扫描下方二维码关注我
(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/28243489
)

[LeetCode] Evaluate Reverse Polish Notation [2]的更多相关文章

  1. LeetCode: Evaluate Reverse Polish Notation 解题报告

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

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

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

  3. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  4. [leetcode]Evaluate Reverse Polish Notation @ Python

    原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...

  5. Leetcode Evaluate Reverse Polish Notation

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

  6. leetcode——Evaluate Reverse Polish Notation 求算式值(AC)

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

  7. [LeetCode] Evaluate Reverse Polish Notation stack 栈

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

  8. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  9. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

随机推荐

  1. mac下解压bin文件

    在mac下要解压Android-ndk-r10e-darwin-x86_64.bin文件. 1.进入文件所在目录,修改文件的读取权限 chmod a+x android-ndk-r10e-darwin ...

  2. ElasticSearch入门之彼行我释(四)

    散仙在上篇文章中,介绍了关于ElasticSearch基本的增删改查的基本粒子,本篇呢,我们来学下稍微高级一点的知识: (1)如何在ElasticSearch中批量提交索引 ? (2)如何使用高级查询 ...

  3. CAS去掉HTTPS认证

    如何去掉HTTPS认证? 说明:默认情况下HTTP也是可以访问CAS SERVER的,但认证,登陆,退出等操作均没有任何的效果.所以必须作出下面的修改 1.进入WEB-INF\spring-confi ...

  4. C++面向对象高级编程(下)-Geekband

    11, 组合和继承 一, Composition 复合  has-a的关系 简单来讲, 就是: class A{     classB b1;   }; 这里讲到Adapter设计模式: templa ...

  5. Top- Linux必学的60个命令

    1.作用 top命令用来显示执行中的程序进程,使用权限是所有用户. 2.格式 top [-] [d delay] [q] [c] [S] [s] [i] [n] 3.主要参数 d:指定更新的间隔,以秒 ...

  6. SQL中distinct 和 row_number() over() 的区别及用法

    1 前言 在咱们编写 SQL 语句操作数据库中的数据的时候,有可能会遇到一些不太爽的问题,例如对于同一字段拥有相同名称的记录,我们只需要显示一条,但实际上数据库中可能含有多条拥有相同名称的记录,从而在 ...

  7. sas单变量的特征分析

    sas单变量的特征分析 大炮,我有个烦恼,我领导最近老叫我单变量结合因变量分析,但是都是分段分析,我总是写proc sql然后group by ,但是这个过程好无聊啊,有木有什么新的代码,让我可以分析 ...

  8. SignalR 2.0 入门与提高 转载https://www.cnblogs.com/vance/p/SignalR.html

    SignalR 2.0 最近整理了SignalR2.0 部分知识点,原文翻译,由于自己是土鳖,翻译得不好的地方,欢迎指正!仅供各位初学者学习! 第一节. 入门ASP.NET SignalR2.0 1. ...

  9. MyBatis与JPA的区别是什么

    MyBatis分为全注解版和xml版:全注解版适合于小项目,直接在方法上加注解,在注解中写sql 仓储Repository 模式是领域驱动设计中另一个经典的模式.在早期,我们常常将数据访问层命名为:D ...

  10. Docker(六)安装Red5进行rtmp推流

    1.pull镜像 docker pull mondain/red5 2.启动原版red5 docker run --name red5 -d -p 5080:5080 -p 1935:1935 mon ...