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

Valid operators are +-*/. Each operand may be an integer or another expression.

Note:

  • Division between two integers should truncate toward zero.
  • The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.

Example 1:

Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

思路

I would use stack to help solving this problem

Traverse the whole given string array

1. if operand is integer,  push into stack

2. if operand is operation, pop two items from stack, do caculation and push result back to stack

code

 class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String s : tokens) {
if(s.equals("+")) {
stack.push(stack.pop()+stack.pop());
}else if(s.equals("/")) {
int latter = stack.pop();
int former = stack.pop();
stack.push( former / latter);
}else if(s.equals("*")) {
stack.push(stack.pop() * stack.pop());
}else if(s.equals("-")) {
int latter = stack.pop();
int former = stack.pop();
stack.push(former - latter);
}else {
stack.push(Integer.parseInt(s));
}
}
return stack.pop();
}
}

[leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法的更多相关文章

  1. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  2. 150. Evaluate Reverse Polish Notation(逆波兰表达式)

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

  3. 150 Evaluate Reverse Polish Notation 逆波兰表达式求值

    求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如:  ["2", "1&quo ...

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

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

  5. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

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

  6. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

  7. leetcode 150. Evaluate Reverse Polish Notation ------ java

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

  8. Evaluate Reverse Polish Notation(逆波兰式)

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

  9. lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

    题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...

随机推荐

  1. jmeter 关联

    (1)正则表达式提取器 注:正则表达式部分配置说明 案例: 动态值:name=userSession value=119828.306682868zVzDzzipfcfDzQicpAVVVf> ...

  2. 打印 laravel 模型查询产品的 SQL

    1.在路由閉包打印sql 打印一段代码生产的 sql 语句,使用路由闭包做个实验 Route::get('/get-sql', function() { DB::enableQueryLog(); $ ...

  3. Python 语言之 map/reduce

    1.相关文献 大名鼎鼎的Google论文<MapReduce: Simplified Data Processing on Large Clusters> 对应的中文翻译<MapRe ...

  4. NodeJs中类定义及类使用

    1.首先定义类Point,文件名为point.class.js: // 定义类 class Point { //构造函数 constructor(x, y) { this.x = x;//类中变量 t ...

  5. 6.8 出口条件循环:do while

    while循环和for循环都是入口条件循环,即在循环的每次迭代之前检查测试条件,所以有可能根本不执行循环体中的内容.C语言还有出口条件循环(exit-condition loop),即在循环的每次迭代 ...

  6. 上线啦,PP.io!

    经过我们PPIO团队成员们在20天零13小时零14分的辛勤努力下,我们的官网终于上线了!   是的,14年前我们就是东半球第一个P2P技术团队.我们的征程始于2004年春天一个温暖的午后.寝室里笨拙的 ...

  7. [UE4]Widget Switcher:控件切换器

    一.Widget Switcher可以有很多子控件,但一次只会显示一个子控件.所有的子控件默认情况下都是充满整个Widget Switcher容器 二.Widget Switcher.Active W ...

  8. [SQL]某数据库中查出包含 字段名 的所有表名

    --利用SQL语句来查询字段所在的表 --从某数据库中查出包含 字段名 字段的所有表名 SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE ...

  9. JS数据类型的判断

    在 ECMAScript 规范中,共定义了 7 种数据类型,分为 基本类型 和 引用类型 两大类,如下所示:    基本类型:String.Number.Boolean.Symbol.Undefine ...

  10. Linux下Memcached安装与启用

    http://www.linuxidc.com/Linux/2016-07/133423.htm