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. net IL的一些探索

    查看IL有2个工具比较好用,一个是大名鼎鼎的Reflector,但已经转向收费,另一个是开源的ILSpy,强大好用,对linq和lambda表达示的支持很好.相关的书籍也很多,比如这本Microsof ...

  2. 神奇的NOIP模拟赛 T1 LGTB 玩扫雷

    LGTB 玩扫雷 在一个n m 的棋盘上,有位置上有雷(用“*” 表示),其他位置是空地(用“.” 表示).LGTB 想在每个空地上写下它周围8 个方向相邻的格子中有几个雷.请帮助他输出写了之后的棋盘 ...

  3. powershell ise好字库和diy配色文件分享

    Windows PowerShell ISE (集成脚本环境) 是 Win中自带的脚本编写工具. 在 Windows PowerShell ISE 中,可以在单个基于 Windows 的图形用户界面中 ...

  4. spark1.3.1安装和集群的搭建

    由于越来越多的人开始使用spark计算框架了,而且spark计算框架也是可以运行在yarn的平台上,因此可以利用单个集群,运行多个计算框架.这是一些大公司都是这么干的.好了,下面讲一下spark1.3 ...

  5. 《day18_String练习_基本类型包装类_集合入门》

    package cn.itcast.api.String.test; public class StringTest_1 { public static void main(String[] args ...

  6. poj2769 暴力

    //Accepted 208 KB 157 ms //纯暴力 //vis数组初始化时要用多少设置多少,不然TLE #include <cstdio> #include <cstrin ...

  7. angularjs 选项卡 --- 自定义属性

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...

  8. C# 跨线程操作控件(简洁)

                                              C# 跨线程操作控件 .net 原则上禁止跨线程访问控件,因为这样可能造成错误的发生.解决此问题的方法有两个: 第一 ...

  9. 自动打补丁Bat指令

    自动打补丁 补丁放于hotfix文件夹下 for %%i in (hotfix\*.exe) do start /wait %%i /z /u

  10. 【转】Polymer API开发指南 (一)(翻译)

    原文转自:http://segmentfault.com/blog/windwhinny/1190000000592324 翻译的不好,轻拍 Polymer是google的一款前端开发框架,其基于Sh ...