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

就是求逆波兰表达式(后续遍历)的结果。

1、直接求解,很慢

public class Solution {
public int evalRPN(String[] tokens) { int len = tokens.length;
if( len == 0)
return 0;
for( int i = 0 ; i < len ; i ++ ){ if( tokens[i].equals("+") )
helper(tokens,i,1);
else if( tokens[i].equals("-") )
helper(tokens,i,2);
else if( tokens[i].equals("*") )
helper(tokens,i,3);
else if( tokens[i].equals("/") )
helper(tokens,i,4);
}
return Integer.valueOf(tokens[0]);
}
public void helper(String[] tokens,int pos,int type){
int pos1 = -1,pos2 = 0 ;
tokens[pos] = null;
while( pos >= 0 ){
if( tokens[pos] != null){
if( pos1 == -1)
pos1 = pos;
else{
pos2 = pos;
break;
}
}
pos--;
}
int num1 = Integer.valueOf(tokens[pos1]);
int num2 = Integer.valueOf(tokens[pos2]);
if( type == 1){
tokens[pos2] = String.valueOf(num1+num2);
}else if( type == 2){
tokens[pos2] = String.valueOf(num2-num1);
}else if( type == 3){
tokens[pos2] = String.valueOf(num2*num1);
}else if( type == 4){
tokens[pos2] = String.valueOf(num2/num1);
}
tokens[pos1] = null;
} }

2、使用栈,很简单。

public class Solution {
public int evalRPN(String[] tokens) { int len = tokens.length; if( len == 0)
return 0;
Stack<Integer> stack = new Stack<Integer>();
for( int i = 0 ; i < len ; i ++ ){ if( tokens[i].equals("+") ){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num1+num2);
}
else if( tokens[i].equals("-") ){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num2-num1);
}
else if( tokens[i].equals("*") ){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num1*num2);
}
else if( tokens[i].equals("/") ){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num2/+num1); }else{
stack.push(Integer.valueOf(tokens[i]));
} }
return stack.pop();
} }

leetcode 150. Evaluate Reverse Polish Notation ------ java的更多相关文章

  1. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

  2. 【Leetcode】Evaluate Reverse Polish Notation JAVA

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

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

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

  4. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

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

  5. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

  6. Leetcode#150 Evaluate Reverse Polish Notation

    原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...

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

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

  8. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  9. 【LeetCode】150. Evaluate Reverse Polish Notation

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

随机推荐

  1. 房子里的K2 BPM业务流程管理

    房…子这件事上,尴尬实在太多. ಥ_ಥ 职场新人,租房很尴尬: 未婚男女,婚房很尴尬: 有下一代的,学区房很尴尬: 耄耋之年,养老房很尴尬... ▽ 甭管买房.租房.装修.设计,关于房子的尴尬事,三天 ...

  2. [转] Android应用程序与SurfaceFlinger服务的关系概述和学习计划

    转自:Android应用程序与SurfaceFlinger服务的关系概述和学习计划 SurfaceFlinger服务负责绘制Android应用程序的UI,它的实现相当复杂,要从正面分析它的实现不是一件 ...

  3. iOS中属性Property的常用关键字的使用说明

    属性关键字的作用 现在我们iOS开发中,基本都是使用ARC(自动引用计数)技术,来编写我们的代码.因此在属性property中我们经常使用的关键字有strong,weak,assign,copy,no ...

  4. php中常用的运算符

    运算符 运算符是告诉PHP做相关运算的标识符号. PHP运算符一般分为算术运算符.赋值运算符.比较运算符.三元运算符.逻辑运算符.字符串连接运算符.错误控制运算符. 1.变量名记得加“$” 符: 2. ...

  5. openstack context

    之前一直不知道context模块中存储的是什么东西,这回看一下代码: 其中最主要的类是:RequestContext: class RequestContext(object):   "&q ...

  6. JS 用window.open()函数,父级页面如何取到子级页面的返回值?

    父窗口:<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> ...

  7. Jul_31 PYTHON REGULAR EXPRESSIONS

    1.Special Symbols and Characters 1.1 single regex 1 . ,Match any character(except \n) ^ ,Match start ...

  8. 极客DIY:使用Arduino制作一块开源手表

    1 – 引言 首先让我们看下这个项目要考虑到的问题: .)使用100%Arduino兼容性硬件 .)保证存储器足够大可以装下大量的稍后会扩展的新内容 .)电量最少够1天用 .)BLE既是中枢设备又是外 ...

  9. What is hmux in resin?

    When I start the Resin server it says hmux listening to localhost:6802 What is this hmux? Is this a ...

  10. N个元素组成二叉树的种类

    <算法>中的二叉查找树一节的一道习题. N个元素组成的二叉树固定一个根节点,这个根节点的左右子树组合数为(0,n-1),(1,n-2),(2,n-3)...(n-1,0),假设N个元素组成 ...