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(逆波兰表示法的计算器)的更多相关文章

  1. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  2. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  3. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  4. Evaluate Reverse Polish Notation(逆波兰式)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

    题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...

  6. 150. Evaluate Reverse Polish Notation(逆波兰表达式)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  7. 150 Evaluate Reverse Polish Notation 逆波兰表达式求值

    求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如:  ["2", "1&quo ...

  8. Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值

    根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...

  9. Java Evaluate Reverse Polish Notation(逆波兰式)

    表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...

  10. Leetcode OJ : Evaluate Reverse Polish Notation Stack C++ solution

    #define ADDITION '+' #define SUBSTRACTION '-' #define MULTIPLICATION '*' #define DIVISION '/' class ...

随机推荐

  1. golang的极简流式编程实现

    传统的过程编码方式带来的弊端是显而易见,我们经常有这样的经验,一段时间不维护的代码或者别人的代码,突然拉回来看需要花费较长的时间,理解原来的思路,如果此时有个文档或者注释写的很好的话,可能花的时间会短 ...

  2. django 中的视图(Views)

    Views Django中views里面的代码就是一个一个函数逻辑, 处理客户端(浏览器)发送的HTTPRequest, 然后返回HTTPResponse, http请求中产生两个核心对象: http ...

  3. numpy.random.seed()

    numpy.random.seed():用于指定随机数生成时使用算法的开始值,如果没有指定每次生成的值都不一样 如果不指定seed的值,那么每次随机生成的数字都不一样: In [17]: import ...

  4. scc

    CSS简介 CSS介绍 CSS(cascading style sheet,层叠样式表)是一种制作网页的新技术,现在已经为大多数浏览器所支持,成为网页设计必不可少的工具之一 CSS语法 CSS实例 每 ...

  5. linux 查看tomcat 日志

    tomcat 重启: cd /opt/appserver/apache-tomcat-/bin ./shutdown.sh -ef|grep tomcat kill - ./startup.sh 查看 ...

  6. PAT 天梯赛 L1-048. 矩阵A乘以B 【数学】

    题目链接 https://www.patest.cn/contests/gplt/L1-048 题意 给出两个矩阵,先判断两个矩阵能不能相乘,如果可以,就输出相乘 结果,如果不行 则按格式输出erro ...

  7. PAT 天梯赛 L1-045. 宇宙无敌大招呼 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-045 AC代码 #include <iostream> #include <cstdio&g ...

  8. 发送邮件——stamplib

    配置文email.ini件信息: [email]sender=xxxxxxxxxxxpwd=xxxxxxxxxxxxreciver=xxxxxxxxxxxxxpython 3.x代码如下: impor ...

  9. centos7 上搭建私有云

    OwnCloud环境搭建 一. 环境搭建 1. 环境需求 服务器操作系统:Centos7.0 外网服务器操作系统:Centos7.0 Php版本号:5.4.16 Mysql版本号:5.5.52 Apa ...

  10. react className 样式控制

    1.<div className={ "formbox " + this.state.classArr }></div> 2. this.state.cla ...