LeetCode-Evaluate Reverse Polish Notation[AC源码]
package com.lw.leet2; /**
* @ClassName:Solution
* @Description:
* 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
*
* @Author LiuWei
* @Date 2014年8月16日上午11:31:05
* @Mail nashiyue1314@163.com
*/
public class Solution { private boolean isOper(String s){
if(s.equals("+")){
return true;
}
if(s.equals("-")){
return true;
}
if(s.equals("*")){
return true;
}
if(s.equals("/")){
return true;
}
return false;
} private String getOperRes(String num1,String num2,String oper){
if(oper.equals("+")){
return Integer.valueOf(num1)+Integer.valueOf(num2)+"";
}
else if(oper.equals("-")){
return Integer.valueOf(num1)-Integer.valueOf(num2)+"";
}
else if(oper.equals("*")){
return Integer.valueOf(num1)*Integer.valueOf(num2)+"";
}
else{
return Integer.valueOf(num1)/Integer.valueOf(num2)+"";
}
} public int evalRPN(String[] tokens) {
String[] resArr = new String[tokens.length];
int index =0;
for(int i=0;i<tokens.length;i++){
if(isOper(tokens[i])){
String num1 = resArr[index-1];
String num2 = resArr[index-2];
resArr[index-2] = getOperRes(num2, num1, tokens[i]);
index--;
}
else{
resArr[index] = tokens[i];
index ++;
}
}
return Integer.valueOf(resArr[0]);
} public static void main(String[] args){
Solution s = new Solution();
// String [] tokens = {"4", "13", "5", "/", "+"};
String [] tokens = {"2", "1", "+"};
System.out.println(s.evalRPN(tokens));
}
}
LeetCode-Evaluate Reverse Polish Notation[AC源码]的更多相关文章
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- leetcode——Evaluate Reverse Polish Notation 求算式值(AC)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- [leetcode]Evaluate Reverse Polish Notation @ Python
原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...
- [LeetCode] Evaluate Reverse Polish Notation [2]
题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, ...
- Leetcode Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] Evaluate Reverse Polish Notation stack 栈
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
随机推荐
- 20170928-3 四则运算psp
1.本周psp: 2.本周进度条: 3.累计进度图(折线图): 4.psp饼状图:
- Week4_Linux书本一二两章
第一章的学习内容就是对Linux内核有一个基本的了解,同时知道一些关于Linux的知识. 学习Linux,可以自己有一台装有Linux操作系统的机器,源代码的作用无可替代: Linux发展历程简介:L ...
- C++Primer第五版——习题答案和解析
感谢原文博主的分享:https://blog.csdn.net/misayaaaaa/article/details/53786215 新手入门必看的书.知识是一个系统化并且相互关联的体系,零散的东西 ...
- 安装Ubuntu 16.04双系统详解(Nvidia显卡)
Ubuntu16.04双系统安装 一.准备工作 设备:惠普台式机,i5-7400.8G内存.1T机械硬盘.NVIDIA GTX1050显卡.预装系统:Win10. 1.下载ubuntu镜像文件,本人使 ...
- Struts2(六)
以下内容是基于导入struts2-2.3.32.jar包来讲的 1.OGNL OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表 ...
- 1014C程序语法树
程序:冒泡算法C程序 #include <stdio.h> main() { int i,j,temp; int a[10]; for(i=0;i<10;i++) scanf (&q ...
- C语言文法阅读与理解
<翻译单元>--><外部声明>--><函数定义>|<申报> <函数定义>--><声明说明符>-->< ...
- MMU 和 MPU的区别
S3C2440里面带的是MMU,而现在流行的Cortex-M3/4 里面带的是MPU. MMU vs MPU 内存是现代计算机最重要的组件之一.因此,它的内容不能被任何错误的应用所篡改.这个功能可以通 ...
- 【week12】psp
psp 项目 内容 开始时间 结束时间 被打断 净时间 12月2日 写博客 对各小组评价 11:20 12:05 0 45 写博客 final评价1 23:40 23:57 0 17 12月5日 看论 ...
- testng几种写法
testng几种写法: 1 <!--运行-类--> 2 <?xml version="1.0" encoding="UTF-8"?> 3 ...