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 json 转换

    在PHP语言中使用JSON   作者: 阮一峰 日期: 2011年1月14日 目前,JSON已经成为最流行的数据交换格式之一,各大网站的API几乎都支持它. 我写过一篇<数据类型和JSON格式& ...

  2. scrum立会报告+燃尽图(第三周第四次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2286 项目地址:https://coding.net/u/wuyy694 ...

  3. PSP DAILY软件功能说明书

    PSP DAILY软件功能说明书 一.开发背景 你在完成了一周的软件工程作业后,需要提交一个PSP图表,里面有4项,如下所示: 1.本周PSP表格,包含每项任务的开始.中断.结束.最终时间,格式如下: ...

  4. Thunder团队第七周 - Scrum会议1

    Scrum会议1 小组名称:Thunder 项目名称:i阅app Scrum Master:杨梓瑞 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...

  5. Thunder--Beta发布--美工+文案

    作业:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/1366 内容: 美工:原有功能展示.新增功能展示 程序图标 欢迎页面 我的书架 ...

  6. 软工网络15团队作业4——Alpha阶段敏捷冲刺-6

    一.当天站立式会议照片: 二.项目进展 昨天已完成的工作: 完成对账单的编辑,删除等操作,以及开始服务器的编写工作 明天计划完成的工作: 记账功能基本完成,进一步优化功能与完善服务器 工作中遇到的困难 ...

  7. 【第八周】【新蜂】新NABCD

    由小组成员宫成荣撰写 一.小组项目申请时提交的NABCD: 痛点:普通的俄罗斯方块是不现实距离下一级有多远的,我们的游戏能显示距离下一等级游戏有多远.方便玩家体验. nabc: n:能满足大多数玩家的 ...

  8. Linux服务器ping不通域名出现的unknown host 错误解决办法

    "ping: unknown host www.baidu.com" 解决方法 如果某台Linux服务器ping不通域名, 如下提示: # ping www.baidu.compi ...

  9. delphi使用SQL的教程4(使用Params属性为参数赋值 )

    17.4.1 使用Params属性为参数赋值   TQuery部件具有一个Params属性,它们在设计时不可用,在程序运行过程中可用,并且是动态建立的,当为TQuery部件编写动态SQL 语句时, D ...

  10. HDU2460-Network

    题目 给一个\(n\)个点\(m\)条边的无向连通图,\(Q\)次往图中加边,每次加边后问图中的桥有多少个.(加边后边留着). \(n\le 10^5,m\le 2\times 10^5,Q\le 1 ...