【后缀表达式求解】No.3.栈-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
注意点
可能存在输入值为负数的情况!其余的就按照后缀表达式来计算就OK了!
开两个栈,数字栈满2和字符串栈不为空就进行一次运算,运算出结果后还放回数字栈!
就酱~~
Java语法写蒜法有点头疼,很多数值转换不如C/C++来的方便,多写写习惯了可能就好了.
题解,仅供参考
import java.util.Stack;
public class Solution {
public int evalRPN(String[] tokens) {
int ans=0;
//操作符栈
Stack<String> op = new Stack<>();
//数字栈
Stack<String> num = new Stack<>();
String opList = "+-*/";
for(int i=0;i<tokens.length;i++){
char ch = tokens[i].charAt(0);
if(tokens[i].length()==1&&opList.indexOf(tokens[i].charAt(0))!=-1){
op.push(tokens[i]);
}
else{
num.push(tokens[i]);
}
//当数字>=2 并且 op栈>=1 时进行计算
while(op.size()>=1&&num.size()>=2){
Integer integer1 = new Integer(num.pop());
Integer integer2 = new Integer(num.pop());
int index = opList.indexOf(op.pop());
switch (index){
case 0:
num.push( String.valueOf(integer1+integer2));
break;
case 1:
num.push( String.valueOf(integer2-integer1));
break;
case 2:
num.push( String.valueOf(integer1*integer2));
break;
case 3:
num.push( String.valueOf(integer2/integer1));
break;
default:
break;
}
}
}
ans = Integer.valueOf(num.pop());
return ans;
}
}
【后缀表达式求解】No.3.栈-evaluate-reverse-polish-notation题解(Java版)的更多相关文章
- Evaluate Reverse Polish Notation leetcode java
题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- 【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】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- 【Spring Boot学习之十】整合Dubbo
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 参考以下两篇文章,总结的很全面: springboot整合最新版dubbo以及dubbo-admin的安装使用Spri ...
- BlackBerry Key2 键盘扩展
概述 BlackBerry Key2 键盘扩展是为BlackBerry Key2输入物理键盘上缺少的键而制作的输入法. BlackBerry Key2键盘和内置输入法是为商业环境而设计的,缺少桌面计算 ...
- Linux 就该这么学 CH03 管道符、重定向和环境变量
0 概述 1 输入输出重定向 重定向技术有5种模式: 标准覆盖输出重定向 标准追加输出重定向 错误覆盖输出重定向 错误追加输出重定向 输入重定向 输入重定向是将文件导入到命令中. 输出重定向是将输入到 ...
- python抓取不得姐动图(报错 urllib.error.HTTPError: HTTP Error 403: Forbidden)
抓取不得姐动图(报错) # -*- coding:utf-8 -*- #__author__ :kusy #__content__:文件说明 #__date__:2018/7/23 17:01 imp ...
- 2019-7-01 python基础数据类型
一.python的注释 python的注释分类: 单行注释: # 单行注释 多行注释:(可以是三个单也可以是三个双) ''' 单三引号多行注释 ''' """ 双 ...
- IDEA 开发插件
Alibaba Java Code Guidelines 阿里巴巴推出的一款Java代码规约扫描插件,按照<阿里巴巴Java开发手册>规定对代码风格以及质量进行实时检测.约束.强推.ecl ...
- APIO2019题解
T1.桥梁(bridges/restriction) Subtask1:暴力,$O(n^2)$. #include<cstdio> #include<algorithm> #d ...
- 如何在Job中获取 IOC applicationcontext
如何在Job中获取 IOC applicationcontext https://segmentfault.com/q/1010000008002800 SpringBoot之整合Quartz调度框架 ...
- Bootstrap中的datetimepicker用法总结
bootstrap时间控件参考文档(少走弯路) https://blog.csdn.net/hizzyzzh/article/details/51212867#31-format-%E6%A0%BC% ...
- ArcGIS Engine中C#开发不能引用ESRI.ArcGIS.AxControls问题
问题:ArcGIS Engine中C#开发不能引用ESRI.ArcGIS.AxControls问题 解决方法:将这里的特定版本改成“False”即可.