牛客网的题目链接

题目描述

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. EF的 NoTracking 的一些记录

    NoTracking官方解释 跟踪与非跟踪查询 跟踪行为可控制 Entity Framework Core 是否将有关实体实例的信息保留在其更改跟踪器中. 如果已跟踪某个实体,则该实体中检测到的任何更 ...

  2. 初识IO流

    输入输出流,用来进行设备之间的数据传输. 是我们IO传输的数据是以文件的形式体现的,所以Java给我们提供了一个类,Flie用来描文件和目录 File(File parent, String chil ...

  3. 使用Prometheus监控bind9的DNS服务

    首先编译bind_exporter,编译方式参见bind_exporter 创建一个systemd配置文件来运行bind_exporter vi /etc/systemd/system/bind_ex ...

  4. static binding/dynamic binding

    static binding/dynamic binding class Person { private void who() { System.out.println("Inside p ...

  5. Java开发笔记(一百四十八)通过JDBC查询数据记录

    前面介绍了通过JDBC如何管理数据库,当时提到Statement专门提供了executeQuery方法用于查询操作,为什么查询操作这么特殊呢?这是因为其它语句跑完一次就了事了,顶多像insert.up ...

  6. FMX 隐藏任务栏 xe10

    找了好多信息,测试好些,xe10  隐藏只要以下命令 隐藏 ShowWindow(ApplicationHWND, SW_HIDE); 显示 ShowWindow(ApplicationHWND, S ...

  7. NFS实现多服务器文件共享

    服务器 1.安装所需的软件包 yum install -y rpc-bind nfs-utils 2.设为开机自启动 systemctl enable nfs systemctl enable rpc ...

  8. react-router的BrowserHistory 和 HashHistory 的区别,如何解决使用BrowserHistory 引起的访问路径问题

    一,使用createBrowserHistory 和 createHashHistory 的 区别体现 1. 使用createBrowserHistory () // 使用createBrowserH ...

  9. 转!!通俗理解数字加密,数字签名,数字证书和https

    原博文地址:https://www.jianshu.com/p/4932cb1499bf 前言 最近在开发关于PDF合同文档电子签章的功能,大概意思就是在一份PDF合同上签名,盖章,使其具有法律效应. ...

  10. Docker 使用入门 Hello World

    结合上一篇的所说的内容:现在安装好了Docker 安装好了就要用嘛,所以,先走一个Hello World docker run ubuntu:15.10 /bin/echo "hello w ...