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. 阿里的Json解析包FastJson使用

    阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征: 速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser ...

  2. ECMAScript各版本简介及特性

    术语 ECMAScript Sun(现在的Oracle)公司持有着“Java”和“JavaScript”的商标.这就让微软不得不把自己的JavaScript方言称之为“JScript”.然后,在这门语 ...

  3. 【C++】C++中的字符和字符串

    目录结构: contents structure [-] 定义和初始化string string对象上的操作 处理string对象中的字符 C风格字符串 标准库类型string表示可变长的字符序列,使 ...

  4. 【Android开发坑系列】之窗口管理

    关键知识要点如下(持续更新): WindowManagerService只负责管理Window,不负责图像的绘制: SurfaceFlinger负责图像的合成:

  5. 游戏行业DDoS攻击解决方案

    行业综述 根据全球游戏和全球移动互联网行业第三方分析机构Newzoo的数据显示:2017年上半年,中国以275亿美元的游戏市场收入超过美国和日本,成为全球榜首. 游戏行业的快速发展.高额的攻击利润.日 ...

  6. 【Shiro】小读Shiro Filter

    类继承结构图 看不明白此图不要紧,后面慢慢提到此图的类: AbstractFilter,抽象过滤器 它实现Filter.继承ServletContextSupport. 它主要实现了init(Filt ...

  7. iptables转发技术

    NAT 一. 什么是 NAT NAT(Network Address Translation)译为网络地址转换.通常路由器在转发我们的数据包时,仅仅会将源MAC地址换成自己的MAC地址,但是NAT技术 ...

  8. .net Core 生产环境报错 MIME

    because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. 主要没 ...

  9. go module下golang.org如何处理被墙

    一.系统提示 go: golang.org/x/sys@v0.0.0-20180905080454-ebe1bf3edb33: unrecognized import path "golan ...

  10. WinForm中一个窗体调用另一个窗体

    [转] WinForm中一个窗体调用另一个窗体的控件和事件的方法(附带源码) //如果想打开一个 Form2 的窗体类,只需要: Form2 form = new Form2(); //有没有参数得看 ...