【Leetcode】Evaluate Reverse Polish Notation JAVA
一、问题描述
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的更多相关文章
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【leetcode刷题笔记】Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- iOS 开发压缩--SSZipArchive
// 解压 NSString *zipPath = @"被解压的文件路径"; NSString *destinationPath = @"解压到的目录"; [S ...
- Java-斐波那契数
1.目标:使用非递归求斐波那契,0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... 2.思路:观察规律得:从第3个数起,把和从为下一个数的加数,把加数作为下一个数的被加数,即三个 ...
- RMI、Hessian、Burlap、Httpinvoker、WebService的比较
RMI.Hessian.Burlap.Httpinvoker.WebService的比较 2(转) [2]Java远程调用方法性能比较 [IT168技术]现在,Java远程调用方法很多,各种方 ...
- Myeclipse 自定义代码自动格式化(ctrl+alt+F)
打开如图界面:preference->java->code style->formatter下的edit... 如设设置java代码多长换行:
- Partitioner
partitioner 是map中的数据映射到不同的reduce时的根据.一般情况下,partitioner会根据数据的key来把数据平均分配给不同的reduce,同时保证相同的key分发到同一个re ...
- 验证工具类 - ValidateUtils.java
验证工具类,提供验证email格式.是否ipv4.是否ipv6.是否中文.是否数字.正则表达式验证的方法. 源码如下:(点击下载 - ValidateUtils.java .commons-lang- ...
- 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件
Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...
- 利用XPath读取Xml文件
之所以要引入XPath的概念,目的就是为了在匹配XML文档结构树时能够准确地找到某一个节点元素.可以把XPath比作文件管理路径:通过文件管理路 径,可以按照一定的规则查找到所需要的文件:同样,依据X ...
- find-right-interval
https://leetcode.com/problems/find-right-interval/ Java里面TreeMap或者TreeSet有类似C++的lower_bound或者upper_b ...
- CSS的display属性
网页设计中最常用的标签p.div.h1-h6(默认为块级元素),span(默认为内联元素) 内联,内嵌,行内属性标签: 1.默认同行可以继续跟同类型标签: 2.内容撑开宽度 3.不支持宽高 4.不支持 ...