Evaluate Reverse Polish Notation
来源:互联网 发布:windows 自动点击 编程 编辑:程序博客网 时间:2024/06/07 08:40
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:
import java.util.Scanner;import java.util.Stack;public class Solution {public static int evalRPN(String[] tokens) {String op3="+-*/";Stack num= new Stack();int result=0;int index=0;int op=0; int op2=0;for(int i=0;i<tokens.length;i++){ if(op3.contains(tokens[i])){ op=Integer.valueOf(num.pop().toString()); op2=Integer.valueOf(num.pop().toString()); index=op3.indexOf(tokens[i]); switch(index) { case 0:result=op2+op;num.push(result);break; case 1:result=op2-op;num.push(result);break; case 2:result=op*op2;num.push(result);break; case 3:if(op==0)return 0; result=op2/op;num.push(result);break; } }else{ num.push(tokens[i]); }}result=Integer.valueOf(num.pop().toString());return result;} public static void main(String[] args) {String exp;String[] exp2;char[] ch=null;int result;Scanner scan=new Scanner(System.in);exp=scan.nextLine();exp2=exp.split(" ");result=evalRPN(exp2); System.out.println(result);}}
0 0
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- MOOC程序设计期中考试1.6
- onCreateDialog方法及示例
- mysql 常用命令
- 黑马程序员_IO流(二)——BufferReader、BufferWriter
- (转载)C++内存地址分配
- Evaluate Reverse Polish Notation
- 多线程
- 修饰器模式
- C# 绘制曲线图,并保存为Gif格式文件 调试通过
- 线段树求LIS并统计最长子序列个数
- 各式各样的正则表达式参考大全
- Uva-10626 Buying Coke DP+记忆化搜索
- sphinx PHP全文检索使用教程
- 聊聊并发(一)深入分析Volatile的实现原理