牛客网的题目链接

题目描述

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版)的更多相关文章

  1. Evaluate Reverse Polish Notation leetcode java

    题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...

  2. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

  3. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  4. 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 ...

  5. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  6. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  7. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  8. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  9. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  10. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

随机推荐

  1. [UE4] Adding a custom shading model

    转自:https://blog.felixkate.net/2016/05/22/adding-a-custom-shading-model-1/ This was written in Februa ...

  2. javascript中的this绑定问题

    this的绑定规则 1 默认绑定: function foo(){ console.log(this.a); } var a = 2 ; foo(); 调用 foo() 的时候其实相当于 window ...

  3. Gradle之dependenciens的各种依赖说明

    implementation:对于使用了该命令编译的依赖,对该项目有依赖的项目将无法访问到使用该命令编译的依赖中的任何程序,也就是将该依赖隐藏在内部,而不对外部公开.api 完全等同于compile指 ...

  4. 在linux上安装redis

    下载Redis安装包 wget  http://download.redis.io/releases/redis-3.2.9.tar.gz 解压Redis安装包 tar -zxvf redis-3.2 ...

  5. Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'xxxxx.Controllers.xxxxController'.

    Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activa ...

  6. Spring学习指南-第二章-Spring框架基础(完)

    第二章 Spring框架基础 面向接口编程的设计方法 ​ 在上一章中,我们看到了一个依赖于其他类的POJO类包含了对其依赖项的具体类的引用.例如,FixedDepositController 类包含 ...

  7. 使用码云或GitHub搭建简单的个人网站

    视频链接:https://www.bilibili.com/video/av64294697 码云: 1.新建一个仓库      ​ 路径名会影响你的个人网站的网址(自行修改),开源(公开),其他默认 ...

  8. 下载并使用MNIST数据集

    TensorFlow提供了一个库,可以直接用来自动下载与安装MNIST. MNIST里包含3个数据集:第一个是训练数据集(mnist.train.images),另外两个分别是测试数据集(mnist. ...

  9. postgreSQL 备份+还原多张表

    -U表示用户 -h表示主机 -p表示端口号 -t表示表名 -f表示备份后的sql文件的名字 -d表示要恢复数据库名 一.打开cmd 进入postgresql安装路径下的bin文件夹,以我的为例: cd ...

  10. python ocr中文识别库 tesseract安装及问题处理

    这个破东西,折腾了快1个小时,网上的教材太乱了. 我解决的主要是windows的问题 先下载exe.(一看到这个,我就有种预感,不妙) https://digi.bib.uni-mannheim.de ...