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. SQL Server 索引介绍

    数据库索引是对数据表中一个或多个列的值进行排序的结构,就像一本书的目录一样,索引提供了在行中快速查询特定行的能力 详细出处参考:http://www.jb51.net/article/30950.ht ...

  2. Problem K 栈

    Description A math instructor is too lazy to grade a question in the exam papers in which students a ...

  3. Sketchup+ArcGIS三维建模与管理

    一.软件安装及其说明 1.需要安装的软件及其安装: 这份报告主要涉及到的有三个需要安装的软件ArcGIS9.3(或9.2) .Sketchup6.0和SketchUp6 ESRI 插件. ArcGIS ...

  4. RPI学习--环境搭建_串口连接

    有两种, 一种是通过MAX2323芯片连接的串口,要接VCC为芯片供电. 另一种是通过PL2302芯片连接的USB,可不接VCC,用电脑USB口为芯片供电. 下面以通过MAX2323方式为例. 1,V ...

  5. jQuery Ajax之load()方法

    jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是load().$.get()和$.post()方法,第3层是$.getScript()和$.getJ ...

  6. SharePoint重置密码功能Demo

    博客地址 http://blog.csdn.net/foxdave 本文将说明一个简单的重置SharePoint用户密码(NTLM Windows认证)的功能如何实现 重置密码功能,实际上就是重置域用 ...

  7. history对象

    1.history对象前进 history.forward() 2.history对象后退 history.back() 3.history对象跳入指定页面 history.go(-1):  //当前 ...

  8. 转载--Ubuntu设置环境变量

    Ubuntu设置环境变量并立即生效(以Ubuntu12.04为例) 标签: UbuntuLinux环境变量 2013-09-12 19:04 9961人阅读 评论(1) 收藏 举报  分类: Ubun ...

  9. 支撑向量机(SVM)

    转载自http://blog.csdn.net/passball/article/details/7661887,写的很好,虽然那人也是转了别人的做了整理(最原始文章来自http://www.blog ...

  10. iOS 在UILabel显示不同的字体和颜色

    转自:http://my.oschina.net/CarlHuang/blog/138363 在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串, ...