题目

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. Leetcode75. Sort Colors颜色分类

    给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...

  2. Collection接口和Map接口

  3. c++ stream操作杂记

    包含简单的读写文件,供初学者入门,stream目前包含三种,终端,文件,内存,注意宽字节. ) ; ) ; ] ; } pasting

  4. hibernate4注解字段为mysql的text

    文章的正文detail就需要设置为text 在getter方法上添加注解 @Lob @Basic(fetch = FetchType.LAZY) @Type(type = "text&quo ...

  5. Python爬虫笔记【一】模拟用户访问之表单处理(3)

    学习的课本为<python网络数据采集>,大部分代码来此此书. 大多数网页表单都是由一些HTML 字段.一个提交按钮.一个在表单处理完之后跳转的“执行结果”(表单属性action 的值)页 ...

  6. 简单易学的机器学习算法——基于密度的聚类算法DBSCAN

    一.基于密度的聚类算法的概述     最近在Science上的一篇基于密度的聚类算法<Clustering by fast search and find of density peaks> ...

  7. idea2018.1.5永久破解过程

    可以根据官网推荐注册idea:http://idea.lanyus.com/ 步骤如下:1 下载破解(crack) jar 包 链接:https://pan.baidu.com/s/1-COPHVJi ...

  8. Linux时间设置命令

    1.date: 语法格式:date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=dates ...

  9. 解决pip安装过慢的问题

    1.pip install scipy -i https://pypi.douban.com/simple/ 2.pip install --index https://pypi.mirrors.us ...

  10. Luogu P1963 [NOI2009]变换序列(二分图匹配)

    P1963 [NOI2009]变换序列 题意 题目描述 对于\(N\)个整数\(0,1, \cdots ,N-1\),一个变换序列\(T\)可以将\(i\)变成\(T_i\),其中\(T_i \in ...