leetcode 逆波兰式求解

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
import java.util.Stack;
public class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<Integer>();
int tmp;
for(int i = 0; i<tokens.length; i++){
if("+".equals(tokens[i])){
int a = stack.pop();
int b = stack.pop();
stack.add(b+a);
}
else if("-".equals(tokens[i])){
int a = stack.pop();
int b = stack.pop();
stack.add(b-a);
}
else if("*".equals(tokens[i])){
int a = stack.pop();
int b = stack.pop();
stack.add(b*a);
}
else if("/".equals(tokens[i])){
int a = stack.pop();
int b = stack.pop();
stack.add(b/a);
}
else{
stack.add(Integer.parseInt(tokens[i]));
} }
return stack.pop();
}
}

根据你波兰式求值。看到逆波兰式可以想到栈,扫描表达式,遇到数字则将数字入栈,遇到运算符,时,则从栈顶弹出两个元素,后弹出的元素在运算符的左边,先弹出的元素在元素符的右边,执行运算,将结果入栈。扫描结束后,栈中的元素只剩下一个,即逆波兰式的值

leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation的更多相关文章

  1. Convert Expression to Reverse Polish Notation

    Given an expression string array, return the Reverse Polish notation of this expression. (remove the ...

  2. [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

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

  3. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

  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]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  6. [leetcode]Evaluate Reverse Polish Notation @ Python

    原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...

  7. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

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

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

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

  9. 【leetcode】Evaluate Reverse Polish Notation

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

随机推荐

  1. centos7.2重新安装yum

    [root@zf-test-web01-4 ~]# yum install rng-toolsLoaded plugins: fastestmirrorLoading mirror speeds fr ...

  2. Centos7中在线/离线安装DockerCE最新版

    Docker在Centos7在线/离线安装 一.在线安装 1.检查系统是否支持,因为Docker 要求 CentOS 系统的内核版本高于 3.10 uname -r 2.确保 yum 包更新到最新 y ...

  3. 福利:42套AI技术视频免费领取

    <福利:33套AI技术视频免费领取> 视频获取方式:请加机器学习和自然语言(QQ群号:436303759)群后,私信群主获取(备注上自己想要获取是视频名称),仅限本群公众号粉丝成员,多套视 ...

  4. Python数据预处理:机器学习、人工智能通用技术(1)

    Python数据预处理:机器学习.人工智能通用技术 白宁超  2018年12月24日17:28:26 摘要:大数据技术与我们日常生活越来越紧密,要做大数据,首要解决数据问题.原始数据存在大量不完整.不 ...

  5. 小程序学习笔记二:页面文件详解之 .json文件

       页面配置文件—— pageName.json 每一个小程序页面可以使用.json文件来对本页面的窗口表现进行配置,页面中配置项会覆盖 app.json 的 window 中相同的配置项. 页面的 ...

  6. Docker 的插件式设计

    http://www.tuicool.com/articles/MnIRZvJ http://uzhima.com/2016/08/02/what-is-docker-volume-plugin/ 在 ...

  7. Docker在windows下的使用【一】

    1.windows按照docker的基本要求 (1)64为操作系统,win7或者更高 (2)支持“ Hardware Virtualization Technology”,并且,“virtualiza ...

  8. Xcode9.2打包图片显示异常解决方案

    链接:https://www.jianshu.com/p/ca0bbb403143來源:简书 在使用Xcode9.2适配iPhone X的过程中遇到了部分图片显示异常(不显示或花掉)的问题.主要分两种 ...

  9. Mac NVM 配置

    1.NVM 简介 NVM(node version manager)是一个可以让你在同一台机器上安装和切换不同版本 node 的工具. GitHub 地址 2.NVM 环境配置 2.1 安装 NVM ...

  10. hive SQL 行转列 和 列转行

    一.行转列的使用 1.问题 hive如何将 a       b       1a       b       2a       b       3c       d       4c       d  ...