一、问题描述

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. My SQL InnoDB 1217 - Cannot delete or update a parent row:aforeign key constraint fals

    InnoDB 允许有外键  MyISAM 不允许有外键 InnoDB修改成MyISAM 证明有外键 一张表如果有其他表的外键关联的是它  它也不能是MyISAM 来自为知笔记(Wiz)

  2. MotionEvent

    getAction() returns a pointer id and an event (i.e., up, down, move) information. getActionMasked()  ...

  3. 【重走Android之路】【Java面向对象基础(三)】面向对象思想

    [重走Android之路][基础篇(三)][Java面向对象基础]面向对象思想   1 面向对象的WWH   1.1 What--什么是面向对象         首先,要理解“对象”.在Thinkin ...

  4. eclipse运行mapreduce报错Permission denied

    今天用在eclipse-hadoop平台上运行map reduce(word count)出错了,错误信息为 org.apache.hadoop.security.AccessControlExcep ...

  5. 《Linux/Unix系统编程手册》读书笔记4

    <Linux/Unix系统编程手册>读书笔记 目录 第7章: 内存分配 通过增加堆的大小分配内存,通过提升program break位置的高度来分配内存. 基本学过C语言的都用过mallo ...

  6. MySQL:创建、修改和删除表

    其实对很多人来说对于SQL语句已经忘了很多,或者说是不懂很多,因为有数据库图形操作软件,方便了大家,但是我们不能忘记最根本的东西,特别是一些细节上的东西,可能你用惯了Hibernate,不用写SQL语 ...

  7. wget https://github.com/xxx/yyy/archive/${commit_hash}.zip

    wget https://github.com/xxx/yyy/archive/${commit_hash}.zip

  8. poj-3616 Milking Time (区间dp)

    http://poj.org/problem?id=3616 bessie是一头工作很努力的奶牛,她很关心自己的产奶量,所以在她安排接下来的n个小时以尽可能提高自己的产奶量. 现在有m个产奶时间,每个 ...

  9. leetcode:Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  10. Cocos2d-x 开发手记

    1.所有的源文件统一新建到Classes里,否则无法找到源文件,这样也便于跨平台编译   2.绘图坐标系,与opengl采用相同坐标系,左下角为原点 纹理坐标系,以左上角为原点   3.最近有在学习C ...