Evaluate Reverse Polish Notation & LeetCode 02
来源:互联网 发布:gblive是什么直播软件 编辑:程序博客网 时间:2024/06/04 19:40
//求逆波兰式,只需要一个栈就可以了
//其中符号是先进先出类型,直接判断即可,不用用栈来寸
//逆波兰式 1 2 + 3 * -->> (1+2)*3=9
//而你如果吧运算符也存进栈
//怎会有 1 2 + 3 * -->> 1+(2*3)=7 则会得到错误结果
//另外无论如何java条件语句一定会执行的,只不过是{}里面的执不执行
//所以你在if中pop()的话,就一定会退栈的
//这是一定要先把栈顶元素球得,存储起来,然后再判断
public class Solution {
class Stack<T> {
private node head=null;
class node{
T item;
node next;
}
public boolean isEmpty(){
return head==null;
}
public void push(T item){//头插法
node oldHead=head;//保存后面连接
head=new node();
head.item=item;
head.next=oldHead;
}
public T pop(){
T s=head.item;
head=head.next;
System.out.println(s+"---出栈");
return s;
}
public T peek(){
return head.item;
}
}
public int evalRPN(String[] tokens) {
Stack<Integer> math=new Stack<Integer>();
//Stack<String> op=new Stack<String>();
for(String s:tokens){
if(s.equals("+"))
math.push(math.pop()+math.pop());
else if (s.equals("*"))
math.push(math.pop()*math.pop());
//另外千万不要忘记,除法和减法具有方向性
else if (s.equals("-"))
math.push(-(math.pop()-math.pop()));
else if (s.equals("/")){
try{
Integer a=math.pop();
Integer b=math.pop();
math.push(b/a);
}catch(Exception e){
return 0;
}
}
else
math.push(Integer.parseInt(s));
}
return math.pop();
}
public static void main(String[] args){
Solution demo=new Solution();
String[] st=("4 13 5 / +").split(" ");
for(String ex:st)
System.out.print(ex+" ");
System.out.println();
System.out.println(demo.evalRPN(st));
}
}
//其中符号是先进先出类型,直接判断即可,不用用栈来寸
//逆波兰式 1 2 + 3 * -->> (1+2)*3=9
//而你如果吧运算符也存进栈
//怎会有 1 2 + 3 * -->> 1+(2*3)=7 则会得到错误结果
//另外无论如何java条件语句一定会执行的,只不过是{}里面的执不执行
//所以你在if中pop()的话,就一定会退栈的
//这是一定要先把栈顶元素球得,存储起来,然后再判断
public class Solution {
class Stack<T> {
private node head=null;
class node{
T item;
node next;
}
public boolean isEmpty(){
return head==null;
}
public void push(T item){//头插法
node oldHead=head;//保存后面连接
head=new node();
head.item=item;
head.next=oldHead;
}
public T pop(){
T s=head.item;
head=head.next;
System.out.println(s+"---出栈");
return s;
}
public T peek(){
return head.item;
}
}
public int evalRPN(String[] tokens) {
Stack<Integer> math=new Stack<Integer>();
//Stack<String> op=new Stack<String>();
for(String s:tokens){
if(s.equals("+"))
math.push(math.pop()+math.pop());
else if (s.equals("*"))
math.push(math.pop()*math.pop());
//另外千万不要忘记,除法和减法具有方向性
else if (s.equals("-"))
math.push(-(math.pop()-math.pop()));
else if (s.equals("/")){
try{
Integer a=math.pop();
Integer b=math.pop();
math.push(b/a);
}catch(Exception e){
return 0;
}
}
else
math.push(Integer.parseInt(s));
}
return math.pop();
}
public static void main(String[] args){
Solution demo=new Solution();
String[] st=("4 13 5 / +").split(" ");
for(String ex:st)
System.out.print(ex+" ");
System.out.println();
System.out.println(demo.evalRPN(st));
}
}
0 0
- Evaluate Reverse Polish Notation & LeetCode 02
- Evaluate Reverse Polish Notation | leetcode
- leetcode-Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode - Evaluate Reverse Polish Notation
- LeetCode | Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation (LeetCode)
- leetcode: Evaluate Reverse Polish Notation
- Leetcode Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation -- LeetCode
- 【LeetCode】Evaluate Reverse Polish Notation
- LeetCode-Evaluate Reverse Polish Notation
- LeetCode:Evaluate Reverse Polish Notation
- LeetCode Evaluate Reverse Polish Notation
- java集合框架图
- HOJ 1760 最大子段和
- 第六周作业1——利用哈夫曼编码英文字母表
- Part3 云GIS软件组件引用
- 手把手教你如何利用SSH来安装IOS越狱APP
- Evaluate Reverse Polish Notation & LeetCode 02
- Windows修改默认路由(Default Gateway)
- opencv之Mat
- MapReduce源码分析之架构分析1
- 数据库事务隔离级别
- 2014_04_14第16周周四工作日志:RegQueryValueEx
- 嵌入式C 面试题
- HashMap的底层实现
- 安装vim 没有 curses.h库