一、问题描述

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 二、分析
该问题是其实是一个后缀表达的计算,这道题实现主要有下面几点注意:
  1、如何将String数组中的操作数与操作符读出来,并加以区分开。
  2、当读出来的是操作数时,将该操作数放入到堆栈(Stack)中
  3、当读出来的是操作符时,从堆栈中取出来两个元素并用此操作符进行计算,并将计算的结果放入到堆栈(Stack)中
package com.edu.leetcode;

import java.util.Stack;

public class EvaluateReversePolishNotation {

    public int evalRPN(String[] tokens) {

        Stack<Integer> stack = new Stack<>();
int result = 0;
for (int i = 0; i < tokens.length; i++) {
char c = tokens[i].charAt(0); // 将字符串的第一元素取出来
if (tokens[i].length()!=1||'0' <= c && c <= '9') { //判断为操作数的标准:1、当字符串的长度大于2时,必定为数字;2、当长度为1时,如果第一个为整数时;
stack.push(Integer.valueOf(tokens[i]).intValue());
} else {
int twoNumber = stack.pop(); //取出栈顶元素为第二操作数
int oneNumber = stack.pop(); //再取出栈顶元素第一操作数
switch (c) { //根据操作数,计算结果
case '*':
result = oneNumber * twoNumber;
break;
case '+':
result = oneNumber + twoNumber;
break;
case '-':
result = oneNumber - twoNumber;
break;
case '/':
if (twoNumber != 0) {
result = oneNumber /twoNumber;
break;
}
else{
System.out.println("\nDivided by 0!");
stack.clear();
}
}
stack.push(result);   //将结果放入到堆栈中
}
}
return stack.pop();
} public static void main(String[] args) {
// TODO Auto-generated method stub
String[] string = { "0","3","/"};
EvaluateReversePolishNotation erpn = new EvaluateReversePolishNotation();
int s = erpn.evalRPN(string);
System.out.println(s);
} }

【Leetcode】Evaluate Reverse Polish Notation JAVA的更多相关文章

  1. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  2. 【leetcode】Evaluate Reverse Polish Notation(middle)

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

  3. leetcode 150. Evaluate Reverse Polish Notation ------ java

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

  4. 【LeetCode练习题】Evaluate Reverse Polish Notation

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

  5. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  6. 【leetcode刷题笔记】Evaluate Reverse Polish Notation

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

  7. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

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

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

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

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

随机推荐

  1. ruby 学习 -- Array --2

    定义: [1, 2, 3] # An array that holds three Fixnum objects [-10...0, 0..10,] # An array of two ranges; ...

  2. I,P,B帧和PTS,DTS的关系

    http://www.cnblogs.com/qingquan/archive/2011/07/27/2118967.html 基本概念: I frame :帧内编码帧 又称intra picture ...

  3. ios开发中超简单抽屉效果(MMDrawerController)的实现

    ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...

  4. PHP组合模式、策略模式

    一.问题 模拟不同课程有不同的收费方式,并且能灵活改变(新增或删减),如讲座可以固定收费也可改为按时收费,研讨会也是. 二.模式简介及关键点 1.在父类代码中使用条件语句是一种退倒,可以用多态来代替条 ...

  5. android 点九PNG技术 适应不同分辨率 完美显示效果

    .9.png是一种非失真性压缩位图图形文件格式.PNG格式是非失真性压缩的,允许使用类似于GIF格式的调色板技术,支持真彩色图像,并具备阿尔法通道(半透明)等特性.现在有很多人使用PNG格式于互联网及 ...

  6. qt之esc键

    Esc键对大家来说实在熟悉不过的了,在Qt中Esc键也会默认的进行一些事件的触发,今天对Esc键测试了一下,突然发现不像我想象的那样,在QDialog中按下Esc键会默认调用reject()方法而不是 ...

  7. CentOS下下删除大量文件

    首先建立50万个文件 ➜ test for i in $(seq 1 500000);do echo text >>$i.txt;done 1. rm ➜ test time rm -f ...

  8. eclipse中maven项目部署到tomcat

    其实maven项目部署到tomcat的方式很多,我从一开始的打war包到tomcat/webapps目录,到使用tomcat-maven插件,到直接使用servers部署,一路来走过很多弯路. 下面就 ...

  9. Android: 启动init.rc 中service的权限问题【转】

    转自:http://www.linuxidc.com/Linux/2011-04/35014.htm 通过property_set("ctl.start", service_xx) ...

  10. MSAA, UIA brief explanation

    MSAA, UIA brief explanation 2014-07-24 Reference [1] MSAA, UIA brief explanation [2] Testing Tools [ ...