【后缀表达式求解】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 ...
随机推荐
- linux 资源
linux 资源查看 cpu 信息 通过/proc/cpuinfo 查看 # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 ...
- Dockerfile语法梳理
Dockerfile语法 我们先来看一下上篇的 Dockerfile #获取base image FROM adoptopenjdk/openjdk8:latest #类似于执行 linux指令 RU ...
- python抓取不得姐动图(报错 urllib.error.HTTPError: HTTP Error 403: Forbidden)
抓取不得姐动图(报错) # -*- coding:utf-8 -*- #__author__ :kusy #__content__:文件说明 #__date__:2018/7/23 17:01 imp ...
- PAT 1098
1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one ...
- 1. Spark GraphX概述
1.1 什么是Spark GraphX Spark GraphX是一个分布式图处理框架,它是基于Spark平台提供对图计算和图挖掘简洁易用的而丰富的接口,极大的方便了对分布式图处理的需求.那么什么是图 ...
- vim 自定义设置
修改系统配置(面对所有用户): root@bogon:~# cd /etc/vim/ root@bogon:/etc/vim# ls vimrc vimrc.tiny root@bogon:/etc/ ...
- k8s 回滚应用
kubectl apply 每次更新应用时 Kubernetes 都会记录下当前的配置,保存为一个 revision(版次),这样就可以回滚到某个特定 revision. 滚动更新是一次只更新一小部分 ...
- CF468C Hack It! 构造
传送门 让人觉得脑子不够用的构造 考虑对于一个区间\([l,r]\)如何让它调整使得最后的结果恰好加上\(1\). 注意到对于一个\(<10^{18}\)的数\(x\),\(f(x+10^{18 ...
- tkinter基础-输入框、文本框
本节内容 了解输入框.文本框的使用方法 利用1制作简易界面 首先明确上面由几个元素组成:该界面由界面标题,输入框.两个按钮.文本框组成. 该界面我们需要实现的功能: 在输入框中输入文字,点击inser ...
- Ambari调整日志级别:How to enable debug logging in Ambari Server and Ambari Agent ?
PURPOSE When troubleshooting Ambari issues, it may be necessary to enable debug logging in the Ambar ...