Evaluate Reverse Polish Notation
来源:互联网 发布:人工智能公司 编辑:程序博客网 时间:2024/06/16 07:46
public class Solution { public int evalRPN(String[] tokens) { // reference http://joycelearning.blogspot.com/2014/03/leetcode-evaluate-reverse-polish.html if(tokens==null || tokens.length <1) return 0; Stack<String> st = new Stack<String>(); String opr = "+-*/"; for(String s: tokens){ if(!opr.contains(s)){ st.push(s); }else{ int n1 = Integer.valueOf(st.pop()); //Integer.valueOf int n2 = Integer.valueOf(st.pop()); switch(s){ case "+": st.push(<span style="color:#ff0000;background-color: rgb(255, 255, 102);">String.valueOf</span>(n2+n1)); break; case "-": st.push(String.valueOf(n2-n1)); break; case "*": st.push(String.valueOf(n2*n1)); break; case "/": if(n1!=0){ st.push(String.valueOf(n2/n1)); break; } } } } return <span style="color:#ff0000;background-color: rgb(255, 255, 102);">Integer.valueOf</span>(st.pop()); }}
注意 转换成string 和integer的用法
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
- android开发-2操作系统安装
- java多线程竞争
- 1042. Shuffling Machine (20)
- Java基础视频教程第02天_学习笔记之语法基础
- Storm学习(一) Storm介绍
- Evaluate Reverse Polish Notation
- 用Sqoop2在Mysql和hadoop导入导出数据
- android+Servlet
- 简单方法求解丑陋数
- BZOJ 2176 Strange string 最小表示法
- 迅雷面试题
- 2-2-r-阅读程序(1)
- [LeetCode Java] 13 Roman to Integer
- hdu 5056 Boring count ( 窗口转移法)