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 ...
随机推荐
- 基于mondrian聚合表的R计算olap开发
原文出处:http://www.cnblogs.com/qiaoyihang/p/7348328.html 最近在做基于Mondrian的olap开发,总结一下! 一. schema构建 1.思考:我 ...
- Python 优雅的操作字典
Python 中的字典是Python中一个键值映射的数据结构,下面介绍一下如何优雅的操作字典. 来源:https://www.linuxzen.com/python-you-ya-de-cao-zuo ...
- Android 属性自定义及使用获取浅析
一.概述 相信你已经知道,Android 可使用 XML 标签语言进行界面的定义.每个标签中有一个一个的属性,这些属性有相应的属性值.例如: <cn.neillee.composedmenu.R ...
- how can i get the source code path && file names from an ELF file(compired with -g)?
https://stackoverflow.com/questions/31333337/how-can-i-get-the-source-code-path-file-names-from-an-e ...
- Qt性能问题
使用Qt库开发通信上位机软件,如串口.CAN总线等,涉及到接收界面高速刷新,会使CPU消耗率过高(20%以上),可能还会卡顿. 具体原因不知道,突然想放弃Qt了 ps: 1.问题出在界面刷新,会占据C ...
- java.lang.ClassNotFoundException: org.apache.commons.discovery.tools.DiscoverSingleton
java.lang.ClassNotFoundException: org.apache.commons.discovery.tools.DiscoverSingleton org.apache.ax ...
- 1001: [BeiJing2006]狼抓兔子
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 12827 Solved: 3044[Submit][ ...
- Linux图形化界面下使用命令进行截图的方法
以前在LINUX里面截图都是直接按print screen键或者 alt + print screen. 但是print screen是整个屏幕, alt + print screen是当前窗口. 想 ...
- JAVA文件下载,页面显示另存为效果
经过测试 firefox.QQ.IE 浏览器是可以的 chrome浏览器不行(直接下载了) 1. 系统框架springmvc+jsp 2. 后台servlet代码 @RequestMapping( ...
- Sharding-Jdbc实现分表分库
Sharding-Jdbc分表分库LogicTable数据分片的逻辑表,对于水平拆分的数据库(表),同一类表的总称.订单信息表拆分为2张表,分别是t_order_0.t_order_1,他们的逻辑表名 ...