[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 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
随机推荐
- Leetcode75. Sort Colors颜色分类
给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...
- Collection接口和Map接口
- c++ stream操作杂记
包含简单的读写文件,供初学者入门,stream目前包含三种,终端,文件,内存,注意宽字节. ) ; ) ; ] ; } pasting
- hibernate4注解字段为mysql的text
文章的正文detail就需要设置为text 在getter方法上添加注解 @Lob @Basic(fetch = FetchType.LAZY) @Type(type = "text&quo ...
- Python爬虫笔记【一】模拟用户访问之表单处理(3)
学习的课本为<python网络数据采集>,大部分代码来此此书. 大多数网页表单都是由一些HTML 字段.一个提交按钮.一个在表单处理完之后跳转的“执行结果”(表单属性action 的值)页 ...
- 简单易学的机器学习算法——基于密度的聚类算法DBSCAN
一.基于密度的聚类算法的概述 最近在Science上的一篇基于密度的聚类算法<Clustering by fast search and find of density peaks> ...
- idea2018.1.5永久破解过程
可以根据官网推荐注册idea:http://idea.lanyus.com/ 步骤如下:1 下载破解(crack) jar 包 链接:https://pan.baidu.com/s/1-COPHVJi ...
- Linux时间设置命令
1.date: 语法格式:date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=dates ...
- 解决pip安装过慢的问题
1.pip install scipy -i https://pypi.douban.com/simple/ 2.pip install --index https://pypi.mirrors.us ...
- Luogu P1963 [NOI2009]变换序列(二分图匹配)
P1963 [NOI2009]变换序列 题意 题目描述 对于\(N\)个整数\(0,1, \cdots ,N-1\),一个变换序列\(T\)可以将\(i\)变成\(T_i\),其中\(T_i \in ...