一、问题描述

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. CentOS安装Chrome

    问题 在CentOS安装Chrome会遇到 libstdc++.so.6(GLIBCXX_3.4.15)(64bit) 依赖失败的问题, 即使下载了最新的libstdc++.so.6(包含GLIBCX ...

  2. android-exploitme(五):不安全的数据存储

    今天我来看看如果android将数据存储在sdcard,它的权限是什么样的 1. 打开emm软件,做一笔转账.

  3. [iOS]提交App报错ERROR ITMS -90207

    前几天上传项目N多次,都跳出这个问题 甚是头痛,于是乎各种搜索 1. 第三方的info.plist里面Executable file这个要删除(自己的不能删哦) 2.检查一下用来做跳转到第三方应用的设 ...

  4. 解决JVM最大内存设置问题

    这里和大家讨论一下如何获得JVM最大内存,在命令行下用java-XmxXXXXM-version命令来进行测试,然后逐渐的增大XXXX的值,如果执行正常就表示指定的内存大小可用,否则会打印错误信息. ...

  5. Python命令行解析库argparse

    2.7之后python不再对optparse模块进行扩展,python标准库推荐使用argparse模块对命令行进行解析. 1.example 有一道面试题:编写一个脚本main.py,使用方式如下: ...

  6. Union的妙用和注意

    对于Union我用的比较少,最近一段时间大多使用Lua,所以复习一下Union Union是共用体,顾名思义,公用一块内存 一块内存不同的访问方式 // 1.数组的便捷访问 // 一块内存两种等价的访 ...

  7. javascript 简繁转换

    js 简繁转换 function copy(ob) { var obj=findObj(ob); if (obj) { obj.select();js=obj.createTextRange();js ...

  8. Windows Services Windows Services的操作

    Windows Services的操作 一.服务的创建: 1.新建项目——Windows服务 2.这是每个人都会犯的错误,新建一个项目后,都会按F5(运行),就会出现如下错误: 3.安装服务有很多种方 ...

  9. flex 4 transition

    <s:transitions> <s:Transition fromState="default"> <s:Parallel> <mx:R ...

  10. 【首先膜拜大湿】poj-2386-Lake Counting-DFS模板题

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16802   Accepted: 8523 De ...