[LeetCode] Evaluate Reverse Polish Notation [2]
题目
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;
}
};
另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Evaluate Reverse Polish Notation [2]的更多相关文章
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- [leetcode]Evaluate Reverse Polish Notation @ Python
原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...
- Leetcode Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode——Evaluate Reverse Polish Notation 求算式值(AC)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] Evaluate Reverse Polish Notation stack 栈
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 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 ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
随机推荐
- gulpfile.js demo
var config = require("./build.config") //获取build.config.js文件里的内容 var gulp = require(" ...
- MapReduce 图解流程超详细解答(2)-【map阶段】
接上一篇讲解:http://blog.csdn.net/mrcharles/article/details/50465626 map任务:溢写阶段 正如我们在执行阶段看到的一样,map会使用Mappe ...
- Numpy数据的操作 * dot() multiply() 的区别
使用numpy时,跟matlab不同: 1.* dot() multiply() 对于array来说,* 和 dot()运算不同 *是每个元素对应相乘 dot()是矩阵乘法 对于matrix来说,* ...
- Windows API 第 10篇 SearchTreeForFile
函数原型:BOOL SearchTreeForFile( PSTR RootPath, //系统查找的起始路径, PSTR InputPathName, ...
- 第五章 Odoo 12开发之导入、导出以及模块数据
大多数Odoo 模块的定义,如用户界面和安全规则,实际是存储在对应数据表中的数据记录.模块中的 XML 和 CSV 文件不是 Odoo 应用运行时使用,而是载入数据表的手段.正是因为这个原因,Odoo ...
- [转]js模块化(一)
java有类文件.Python有import关键词.Ruby有require关键词.C#有using关键词.PHP有include和require.CSS有@import关键词,但是对ES5版本的ja ...
- dubbo入门学习(四)-----dubbo配置
配置来源 首先,从Dubbo支持的配置来源说起,默认有四种配置来源: JVM System Properties,-D参数 Externalized Configuration,外部化配置 Servi ...
- PAT甲级——A1038 Recover the Smallest Number
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- jsp导出的word默认打开是web视图,希望是页面视图
方法1 ( velocity+java )我也遇到了这个问题,已经解决:1 .<html xmlns:v='urn:schemas-microsoft-com:vml'xmlns:o='urn: ...
- GYM100633J. Ceizenpok’s formula 扩展lucas模板
J. Ceizenpok’s formula time limit per test 2.0 s memory limit per test 256 MB input standard input o ...