package com.lw.leet2;

 /**
* @ClassName:Solution
* @Description:
* 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
*
* @Author LiuWei
* @Date 2014年8月16日上午11:31:05
* @Mail nashiyue1314@163.com
*/
public class Solution { private boolean isOper(String s){
if(s.equals("+")){
return true;
}
if(s.equals("-")){
return true;
}
if(s.equals("*")){
return true;
}
if(s.equals("/")){
return true;
}
return false;
} private String getOperRes(String num1,String num2,String oper){
if(oper.equals("+")){
return Integer.valueOf(num1)+Integer.valueOf(num2)+"";
}
else if(oper.equals("-")){
return Integer.valueOf(num1)-Integer.valueOf(num2)+"";
}
else if(oper.equals("*")){
return Integer.valueOf(num1)*Integer.valueOf(num2)+"";
}
else{
return Integer.valueOf(num1)/Integer.valueOf(num2)+"";
}
} public int evalRPN(String[] tokens) {
String[] resArr = new String[tokens.length];
int index =0;
for(int i=0;i<tokens.length;i++){
if(isOper(tokens[i])){
String num1 = resArr[index-1];
String num2 = resArr[index-2];
resArr[index-2] = getOperRes(num2, num1, tokens[i]);
index--;
}
else{
resArr[index] = tokens[i];
index ++;
}
}
return Integer.valueOf(resArr[0]);
} public static void main(String[] args){
Solution s = new Solution();
// String [] tokens = {"4", "13", "5", "/", "+"};
String [] tokens = {"2", "1", "+"};
System.out.println(s.evalRPN(tokens));
}
}

LeetCode-Evaluate Reverse Polish Notation[AC源码]的更多相关文章

  1. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  2. leetcode——Evaluate Reverse Polish Notation 求算式值(AC)

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

  3. [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

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

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

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

  5. [leetcode]Evaluate Reverse Polish Notation @ Python

    原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...

  6. [LeetCode] Evaluate Reverse Polish Notation [2]

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

  7. Leetcode Evaluate Reverse Polish Notation

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

  8. [LeetCode] Evaluate Reverse Polish Notation stack 栈

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

  9. 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 ...

随机推荐

  1. 后端编程语言PHP

    | 版权声明:本文为博主原创文章,未经博主允许不得转载. 一.PHP 简介 PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言. PHP 脚本在服务器上执行. 什么是 PHP?(超文本预处理器 ...

  2. 作业要求20181113-4 Beta阶段第1周/共2周 Scrum立会报告+燃尽图 03

    作业要求:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2385 版本控制:[https://git.coding.net/lglr201 ...

  3. OA--对于每个form表单(<s:iterator>生成)的处理

    由于是后台传过来的,我们不知道form 有几个 也不能指定form的id和name,(其实也可以就是可能会冲突我们还是用下面讲的方法把) 之前有想过 对于每个form 里面都有一些参数,举个例子 项目 ...

  4. lintcode-394-硬币排成线

    394-硬币排成线 有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? 样例 n = 1, 返回 ...

  5. Struts2:<s:action>的使用

    <s:action name=”actionName” namespace=”/” executeResult=”true”> <s:action>可以在jsp中直接调用act ...

  6. 【leetcode】54.Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  7. php 计算本周星期一、本月第一天 本月最后一天 下个月第一天

    本周一echo date('Y-m-d',(time()-((date('w')==0?7:date('w'))-1)*24*3600)); //w为星期几的数字形式,这里0为周日 本周日 echo  ...

  8. tooltips & click copy

    tooltips & click copy shit antd & tooltips & click copy https://codesandbox.io/s/zx4wo7y ...

  9. 第147天:web前端开发中的各种居中总结

    一.水平居中 方法① :行内元素 (父元素)text-align,(子元素)inline-block .parent{text-align: center;} .child{display: inli ...

  10. UVA11625_Lines of Containers

    题意很简单,给你一个n*m的矩阵,现在问你这个矩阵能否变为标准矩阵(即数字从小到大),如果能最少需要几步呢? 其实是个赤果果的水题.记得暑假安叔也出过一个类似的题目,那个好像是在codeforces上 ...