牛客网的题目链接

题目描述

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. Kubernetes之使用kubeadm部署

    参考:https://www.cnblogs.com/caoxb/p/11243472.html 部署虚拟机规划 192.168.1.11 k8s-master 192.168.1.12 k8s-no ...

  2. ANR无法生成traces.txt文件

    在有些设备上ANR无法生成traces.txt文件,kill -3又不能准确把握时机或者没权限,可能是软件访问这个路径失败,可以在/data/anr/下面建立一个空的traces.txt,注意保证an ...

  3. sql server 2008 自动备份

    身份验证:包含Windows身份验证和 SQL Server身份验证,此处选择Windows 身份验证; 选择[管理]-->[维护计划]-->[维护计划向导] 必须启用代理服务(启动模式请 ...

  4. Vue 搭建项目

    Vue  搭建项目 一.node下载安装: 1.下载:https://nodejs.org/en/download/ 2.安装默认许选择,下一步就行: 3.安装完之后就可以使用npm命令 二.通过@v ...

  5. activiti学习3:流程引擎对象和流程引擎配置对象

    目录 activiti学习3:流程引擎对象和流程引擎配置对象 一.activiti的简单使用流程 二.流程引擎配置对象ProcessEngineConfiguration的介绍 三.activiti配 ...

  6. IDEA更改JavaScript版本

    最好改两个地方 File -> File -> -- --

  7. linux查看openssh和openssl版本

    查看 openssh 版本命令 ssh -V 查看 openssl 版本命令 openssl version

  8. C语言 hello

    #include <stdio.h> int main() { /* 我的第一个 C 程序 */ printf("Hello, World! \n"); ; } 实例解 ...

  9. Http、RESTful、RPC、MQ、Socket 概念与区别

    若要转载本文,请务必声明出处:https://www.cnblogs.com/zhongyuanzhao000/p/11700815.html 1. 关于HTTP: HTTP,即超文本传输协议,是一个 ...

  10. centos 6.5安装zabbix 4.4

    一.安装环境 本环境,使用单机部署. 操作系统:centos 7.5 x64zabbix-server,Mysql,php,nginx都在同一台服务器.都是使用Yum安装的! 官方安装文档: http ...