LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)
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:
逆波兰表示法计算机,很简单 ,就是一个stack 由符号弹出计算在填入就行了 最后top上的就是计算的最后的结果。
class Solution {
public:
int evalRPN(vector<string>& tokens) {
int sz = tokens.size();
stack<int>result;
int leftEval, rightEval;
for (int i = ; i < sz; ++i){
if (tokens[i].size() == && !isdigit(tokens[i][])){ //处理应该注意,防止出现负数
rightEval = result.top(), result.pop();
leftEval = result.top(), result.pop();
switch (tokens[i][]){
case '+':
result.push(leftEval + rightEval);
break;
case '-':
result.push(leftEval - rightEval);
break;
case '*':
result.push(leftEval * rightEval);
break;
case '/':
result.push(leftEval / rightEval);
break;
default:
break;
}
}
else{
if (tokens[i][] == '-'){
result.push( - atoi(tokens[i].substr(, tokens[i].size() - ).c_str()));
cout << result.top();
}
else
result.push(atoi(tokens[i].c_str()));
}
}
return result.top();
}
};
java版本的方法具体的和上面的差不多,但是java的栈的api用起来确确实实的要容易很多,代码如下所示:
public class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> s = new Stack<Integer>();
for(int i = 0; i < tokens.length; i++){
if(isDigit(tokens[i])){
s.push(Integer.parseInt(tokens[i]));
}else{
int right = s.pop();//分别是左右操作数
int left = s.pop();
switch(tokens[i].charAt(0)){
case '+':
s.push(left + right);
break;
case '-':
s.push(left - right);
break;
case '*':
s.push(left * right);
break;
case '/':
s.push(left / right);
break;
}
}
}
return s.pop();
}
public boolean isDigit(String str){
return Character.isDigit(str.charAt(0))||(str.length() > 1);//防止出现-1这种特殊的数字也需要正确的判断
}
}
LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)的更多相关文章
- [leetcode]150. 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 ...
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
- Evaluate Reverse Polish Notation(逆波兰式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
- 150. Evaluate Reverse Polish Notation(逆波兰表达式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150 Evaluate Reverse Polish Notation 逆波兰表达式求值
求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如: ["2", "1&quo ...
- Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- Java Evaluate Reverse Polish Notation(逆波兰式)
表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...
- Leetcode OJ : Evaluate Reverse Polish Notation Stack C++ solution
#define ADDITION '+' #define SUBSTRACTION '-' #define MULTIPLICATION '*' #define DIVISION '/' class ...
随机推荐
- Mycat教程---数据库的分库分表
mycat介绍 介绍在官方网站上有比较详细的介绍,在这里复制粘贴没什么意思,大家到官网上看 官网链接 前置条件 本教程是在window环境下运行的,实际生产推荐在Linux上运行. 必备条件(自行安装 ...
- rsync+inotify实时同步
!!!在安装前要先确保,rsync daemon服务配置成功,在安装inotify-tools前先确认你的linux内核是否达到了2.6.13,并且在编译时开启CONFIG_INOTIFY选项,也可以 ...
- 访问url
你可以通过 args 属性来访问 URL 中提交的参数 ( ?key=value ): searchword = request.args.get('q', '') from flask import ...
- ArcGIS COM Exception 0x80040228
问题: string shpDir = Path.GetDirectoryName(shpfile); string shpfilename = Path.GetFileNa ...
- Python Tornado框架(ioloop对象分析)
网上都说nginx和lighthttpd是高性能web服务器,而tornado也是著名的高抗负载应用,它们间有什么相似处呢?上节提到的ioloop对象是如何循环的呢?往下看. 首先关于TCP服务器的开 ...
- HDOJ 2203 亲和串 【KMP】
HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4292 Food (拆点最大流)
题意:N个人,F种食物,D种饮料,给定每种食物和饮料的量.每个人有自己喜欢的食物和饮料,如果得到自己喜欢的食物和饮料才能得到满足.求最大满足的人数. 分析:如果只是简单地N个人选择F种食物的话可以用二 ...
- web标准的理解
首先,什么是web标准?web标准是w3c组织为解决跨浏览器兼容问题而推出的关于网页开发时应遵守的规范.在网页的四个部分中网页的内容是由网页开发者自己定义的,因此这一部分无法标准化,而网页的结构(HT ...
- $微信小程序开发实践点滴——Bmob常用API的使用
Bmob后端云官网:http://www.bmob.cn/ Bmob后端云微信小程序开发文档:http://docs.bmob.cn/data/wechatApp/b_developdoc/doc/i ...
- Java 基础总结(二)
本文参见:http://www.cnblogs.com/dolphin0520/category/361055.html 1. 字节流与和字符流 1). 字符流操作时使用了缓冲区,而在关闭字符流时会强 ...