一、问题描述

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. 《jQuery风暴》第2章 必须知道的JavaScript知识

    第2章 必须知道的JavaScript知识 JavaScript是jQuery应用的基础,掌握JavaScript这门语言是使用jQuery的基础条件.本章不会全面细致的讲解JavaScript的全部 ...

  2. Winsock IOCP模型(四篇)

    http://blog.csdn.net/visualeleven/article/details/6041893 http://blog.csdn.net/visualeleven/article/ ...

  3. IDL_GUI

    菜单栏设计 PRO IDLGui ;构建界面 ;显示 ;添加事件 tlb=WIDGET_BASE(xsize=400,ysize=400,/column,mbar=mbar);实现基类 file=WI ...

  4. BigDecimal进行除法divide运算注意事项

     Java编程中  BigDecimal进行除法divide运算时,如果结果不整除,出现无限循环小数.则会抛出以下异常: java.lang.ArithmeticException: Non-term ...

  5. c# 计算1-100之间的所有整数的和

    求1-100所有整数和: class Program { static void Main(string[] args) { ,); Console.WriteLine("1-100之间所有 ...

  6. itnesse实现api接口自动化测试学习

    上午在园子里乱逛,看了不少小伙伴们分享的接口测试方面的知识,大家所叙述到的一些经验或多或少,我也曾遇到过,突然意识到知识的点滴积累是多么的重要,我记得我最早接触接口测试的时候,就是只在浏览器里人工测试 ...

  7. ViewState压缩技术

    ViewState 的使用,大家可以说是又爱又恨,它其中一个特性就是保存页面的状态,对于只是展示的页面,我们可以直接在页面文件中使用 EnableViewState="false" ...

  8. JS 原型链学习总结

    废话篇: 在js的学习过程中有一大难点就是原型链.学习的时候一直对这一内容不是十分的明白.纠结的我简直难受.,幸好总算给他弄通了,哇咔咔,总算可以不用在睡梦中还想着他了. 正文篇: 要了解原型链我们首 ...

  9. 基于jQuery的日历插件

    上个星期看到同事做一个有关日历提醒功能的需求,为了找个插件也是费了不少心思,然后刚好有时间就试着写了一个简单demo 来看下最终效果图吧: 是长得丑了一点,不要吐槽我-.- 首先来说说这个日历主要的制 ...

  10. UVa 12627 (递归 计数 找规律) Erratic Expansion

    直接说几个比较明显的规律吧. k个小时以后,红气球的个数为3k. 单独观察一行: 令f(r, k)为k个小时后第r行红气球的个数. 如果r为奇数,f(r, k) = f((r+1)/2, k-1) * ...