leetcode 150. Evaluate Reverse Polish Notation ------ java
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的更多相关文章
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
- Leetcode#150 Evaluate Reverse Polish Notation
原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- debug实战:进程Hang+High CPU
最近几周都在解决程序不稳定的问题,具体表现为程序(多进程)时不时的Hang住,同时伴随某个进程的High CPU.跟踪下来,基本都是各种死锁引起的.这里选取一个典型的场景进行分析. 1.抓dump分析 ...
- [开发笔记]-DataGridView控件中自定义控件的使用
最近工作之余在做一个百度歌曲搜索播放的小程序,需要显示歌曲列表的功能.在winform中采用DataGirdView来实现. 很久不写winform程序了,有些控件的用法也有些显得生疏了,特记录一下. ...
- Java 类的一些高级特征
1. 面向对象的特征二:继承性 * 1.为什么要设计继承性? 继承的出现提高了代码的复用性. 继承的出现让类与类之间产生了关系,提供了多态的前提. * 2.通过"class A extend ...
- Rhel7的基本使用
1.修改主机名 [root@localhost ~]# cat /etc/hostname localhost.localdomain[root@localhost ~]# hostnamectl s ...
- NIO基础
通道和缓冲区 概述 通道 和 缓冲区 是 NIO 中的核心对象,几乎在每一个 I/O 操作中都要使用它们. 通道是对原 I/O 包中的流的模拟.到任何目的地(或来自任何地方)的所有数据都必须通过一个 ...
- 赋值运算符、拷贝初始化和this指针
一.赋值运算符和拷贝构造函数(重载技术) 赋值运算符和拷贝构造函数有编译器默认提供,但如果想做更复杂的事,需要重载. 1.下面用一个简单的例子先区分一下赋值运算符和拷贝构造函数: #include&l ...
- Sketchup+ArcGIS三维建模与管理
一.软件安装及其说明 1.需要安装的软件及其安装: 这份报告主要涉及到的有三个需要安装的软件ArcGIS9.3(或9.2) .Sketchup6.0和SketchUp6 ESRI 插件. ArcGIS ...
- Android安全之WebViewUXSS漏洞
Android安全 WebView UXSS app开发 漏洞分析 移动安全 0X01 前言 XSS是我们比较熟悉的一种攻击方式,包括存储型XSS.反射型XSS.DOM XSS等,但UXSS(通用型X ...
- (转)phoneGap-Android开发环境搭建
(原)http://www.cnblogs.com/shawn-xie/archive/2012/08/15/2638480.html phoneGap-Android开发环境搭建 一.安装 在安 ...
- android avoiding-memory-leaks
android avoiding-memory-leaks Memory Leak是会有多个方面会引起的,比如Drawable, RemoteViews, Receiver, Cursor,Input ...