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

计算逆波兰表达式,使用一个栈来存储操作数字,遍历输入的链表。

如果为数字,直接push到栈内,如果为操作符号,就把栈顶的两个元素pop出来运算一下。

运算的结果push到栈内。

public class Solution {
public int evalRPN(String[] tokens) {
int result = 0;
Stack<Integer> stack = new Stack<Integer>();
String operators = "+-*/"; for(int i=0; i<tokens.length;i++){
if (operators.contains(tokens[i])){
int b = stack.pop();
int a = stack.pop();
stack.push(calculate(a,b,tokens[i]));
}else{
stack.push(Integer.valueOf(tokens[i]));
}
}
result = stack.pop();
return result;
} public int calculate(int a, int b, String operator){
char op = operator.charAt(0);
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}
return 0;
}
}

leetcode150 Evaluate Reverse Polish Notation的更多相关文章

  1. Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值

    根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...

  2. 【leetcode】Evaluate Reverse Polish Notation

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

  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练习题】Evaluate Reverse Polish Notation

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

  6. leetcode - [2]Evaluate Reverse Polish Notation

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

  7. 【LeetCode】150. Evaluate Reverse Polish Notation

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

  8. LeetCode: Evaluate Reverse Polish Notation 解题报告

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

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

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

随机推荐

  1. ServiceStack.Text 更快的序列化

    Json.net 是以前最经常用的序列化组件,后来又注意到ServiceStack号称最快的,所以我做了以下测试 1)Json.net using System; using System.Colle ...

  2. c#多态之抽象类与接口的一点收获~~

    多态之抽象类与接口的相似点及不同点,刚学习的一点收获,或许不是很完整,借鉴看视频及一些被人写的文章自己写的下的一些心得!以便之久复习使用! 一.抽象类 (1) 抽象方法只作声明,而不包含实现,可以看成 ...

  3. string find

    string类的查找函数: ) const;//从pos开始查找字符c在当前字符串的位置 ) const;//从pos开始查找字符串s在当前串中的位置 int find(const char *s, ...

  4. Java数据结构——容器总结

    4大容器——List.Set.Queue.Map List 1.ArrayList 优点:随机访问元素 缺点:插入和移除元素时较慢 2.LinkedList 优点:插入和删除元素 缺点:随机访问方面相 ...

  5. BigDecimal类

    如果需要精确的计算结果,则必须使用BigDecimal类,而且使用BigDecimal类也可以进行大数的操作. //========================================== ...

  6. Autofac IContainer 测试

    using Autofac; using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  7. clearfix--清除浮动

    .clearfix { zoom: ; display: table; width: %; } .clearfix:after { content: " "; display: b ...

  8. XSHELL下直接下载文件到本地(Windows)

    xshell很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz 首先你的Ubuntu需要安装rz.sz(如果没有安装请执行以下命令,安装完的请跳过. ...

  9. ThinkPHP的四种URL模式 URL_MODEL

    ThinkPHP支持四种URL模式,可以通过设置URL_MODEL参数来定义,包括普通模式.PATHINFO.REWRITE和兼容模式. 普通模式 设置URL_MODEL 为0 采用传统的URL参数模 ...

  10. PHP弹出提示框并跳转到新页面即重定向到新页面

    本文为大家介绍下使用PHP弹出提示框并跳转到新页面,也就是大家所认为的重定向,下面的示例大家可以参考下   这两天写一个demo,需要用到提示并跳转,主要页面要求不高,觉得没必要使用AJAX,JS等, ...