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 ...
随机推荐
- PHP中定义常量
PHP中定义常量的方式如下: define(常量名,常量值); //定义常量PUBLISHER define('PUBLISHER', "O'Reilly & Associates& ...
- 作业 20181016-1 Alpha阶段贡献分配规则
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2244 条件:八位同学,总共80分贡献分(贡献分总数以实际为准),投票方式 ...
- struts2--文件上传大小
Struts2文件上传的大小限制问题 问题:上传大文件报错-- 解决:修改struts.xml文件中的参数如下 <constant name="struts.multipart.max ...
- SSM整合CRUD操作(一)
http://www.cnblogs.com/loger1995/p/6352179.html?utm_source=itdadao&utm_medium=referral 说明:这是我刚开始 ...
- Oracle数据库表空间常用操作
1. 查看所有表空间大小 SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_files group by tables ...
- java 基础 --Collection(Set)
注意: 如果hashSet存储自定义对象,一定要重写hashCode()&&equals() 如果TreeSet存储自定义对象,让元素所属的类实现自然排序接口Comparable,并重 ...
- 数组去重复及记录重复个数(以及遍历map的四种方法)
private static void check(String[] array) { // 字符串数组中,含有不重复的字符串有哪些?每一个重复的个数 Map<String,Integer> ...
- default.properties文件
在地址栏访问某个 action 之所以能访问到,只因为在 default.properties 配置文件中有一个键值对,key 为struts.action.extension,值为 action,, ...
- postman 断言学习
请求 url :https://www.v2ex.com/api/nodes/show.json?name=python get请求 postman发起请求并做断言 断言: tests["B ...
- HDU 6035 Colorful Tree(dfs)
题意:一棵有n个点的树,树上每个点都有颜色c[i],定义每条路径的值为这条路径上经过的不同颜色数量和.求所有路径的值的和. 可以把问题转化为对每种颜色有多少条不同的路径至少经过这种颜色的点,然后加和. ...