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. JavaEE中为什么出现中文乱码?

    1.原因 客户端通过IE提交时用的默认编码是UTF-8,而当我们用Myeclipse的时候的服务端接受的时候用的是iso-8859-1 2.解决方法 服务端也用UTF-8编码 byte[] resul ...

  2. 系列文章(三):WAPI为无线局域网WLAN安全而生——By Me

    导读:无线局域网(又称为WLAN,Wireless Local Area Network),其应用领域不断拓展,无线接入所具有的前所未有的连接性和自动化能够为人们带来巨大的便利和商机.与此同时,在信息 ...

  3. 解读tensorflow之rnn 的示例 ptb_word_lm.py

    这两天想搞清楚用tensorflow来实现rnn/lstm如何做,但是google了半天,发现tf在rnn方面的实现代码或者教程都太少了,仅有的几个教程讲的又过于简单.没办法,只能亲自动手一步步研究官 ...

  4. MariaDB日志

    1.查询日志:一般来说不开开启(会产生额外压力,并且不一定有价值),query log 记录查询操作:可以记录到文件(file)中也可记录到表(table)中 general_log=ON|OFF g ...

  5. day11 函数和命名空间

    # def my_sum(*args):# sum_2=0# for i in args:# sum_2+=i# return sum_2### ret=my_sum(1,2,3,4)# print( ...

  6. LeetCode:逆波兰表达式求值【150】

    LeetCode:逆波兰表达式求值[150] 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除 ...

  7. https部署

    准备证书及秘钥 方式一.springboot项目可直接在yml中配置 1.需要将证书转换成jks或p12格式,如 多个crt证书转为pem: cat xxx.crt xxx2.crt xxx3.xrt ...

  8. springmvc ExceptionHandler

    /** * 1. 在 @ExceptionHandler 方法的入参中可以加入 Exception 类型的参数, 该参数即对应发生的异常对象 * 2. @ExceptionHandler 方法的入参中 ...

  9. ES6 随记(3.3)-- 数组的拓展

    上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 3. ES6 随记(3.1)-- 字符串的拓展 4. ES6 随记(3.2)-- 正则的拓展 ...

  10. 20145235李涛《网络对抗》Exp8 Web基础

    基础问答 什么是表单 可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 表单包括两个部分:一部分是HTML源代码用于描述表单(例如,域,标签和用户在页面上看见的按钮),另一部分是脚本 ...