150 Evaluate Reverse Polish Notation 逆波兰表达式求值
求在 逆波兰表示法 中算术表达式的值。
有效的运算符号包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰计数表达。
例如:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
详见:https://leetcode.com/problems/evaluate-reverse-polish-notation/description/
Java实现:
逆波兰表达式就是把操作数放前面,把操作符后置的一种写法,通过观察可以发现,第一个出现的运算符,其前面必有两个数字,当这个运算符和之前两个数字完成运算后从原数组中删去,把得到一个新的数字插入到原来的位置,继续做相同运算,直至整个数组变为一个数字,这种解法会超时。完美的解法是使用栈,从前往后遍历数组,遇到数字则压入栈中,遇到符号,则把栈顶的两个数字拿出来运算,把结果再压入栈中,直到遍历完整个数组,栈顶数字即为最终答案。
class Solution {
public int evalRPN(String[] tokens) {
if(tokens.length<1){
return 0;
}
Stack<Integer> stack =new Stack<Integer>();
for(String token:tokens){
if(token.equals("+")){
int num1=stack.pop();
int num2=stack.pop();
stack.push(num1+num2);
}
else if(token.equals("*")){
int num1=stack.pop();
int num2=stack.pop();
stack.push(num1*num2);
}
else if(token.equals("/")){
int num1=stack.pop();
int num2=stack.pop();
stack.push(num2/num1);
}
else if(token.equals("-")){
int num1=stack.pop();
int num2=stack.pop();
stack.push(num2-num1);
}
else{
stack.push(Integer.parseInt(token));
}
}
return stack.pop();
}
}
150 Evaluate Reverse Polish Notation 逆波兰表达式求值的更多相关文章
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
- Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
- 150. Evaluate Reverse Polish Notation(逆波兰表达式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [leetcode]150. 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 ...
- Evaluate Reverse Polish Notation(逆波兰式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java Evaluate Reverse Polish Notation(逆波兰式)
表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
随机推荐
- Kubernetes实战阅读笔记--2、架构和部署
安装Kubernetes “本书准备了4台虚拟机(CentOS 7.0系统)用于部署Kubernetes运行环境,包括一个Etcd.一个Kubernetes Master和三个Kubernetes N ...
- 关闭mongodb 集群
[root@hadoop1 ~]# ps -aux | grep mongodb; root ? Sl : : /usr/local/mongodb/bin/mongod -f /usr/local/ ...
- 记录Linux常用命令
创建用户:useradd -m user1,-m表示同时创建用户主目录,默认会创建/home/user1目录 设置密码:passwd user1,然后就会出现设置密码的提示了 为新用户添加sudo权限 ...
- Scrapy运行报错:ModuleNotFoundError: No module named 'douban.douban'
运行scrapy爬虫报错: from douban.douban.items import DoubanItem ModuleNotFoundError: No module named 'douba ...
- PostgreSQ 连接问题 FATAL: no pg_hba.conf entry for host
PostgreSQ数据库为了安全,它不会监听除本地以外的所有连接请求,当用户通过JDBC访问是,会报一些如下的异常: org.postgresql.util.PSQLException: FATAL: ...
- Python-day-9- RabbitMQ队列
RabbitMQ队列 安装 http://www.rabbitmq.com/install-standalone-mac.html 安装python rabbitMQ module pip insta ...
- You can't specify target table 'xxx' for update in FROM clause
1.执行sql语句报上面的错误: DELETE FROM db_student WHERE RowGuid IN ( SELECT RowGuid FROM db_student WHERE age ...
- Event Handling Guide for iOS--(三)---Event Delivery: The Responder Chain
Event Delivery: The Responder Chain 事件传递:响应链 When you design your app, it’s likely that you want to ...
- 输出文章段落首行空格缩进在IE和chrome显示不一致的问题
一般的编辑文章时,首行都缩进两格,而执行的操作则是一个tab键或者四个空格键,在html代码中体现的往往都是4个 然而我在输出时却发现了同样的html代码,在IE上显示的是缩进了一个字符,在chrom ...
- CodeForces 719A Vitya in the Countryside (水题)
题意:根据题目,给定一些数字,让你判断是上升还是下降. 析:注意只有0,15时特别注意一下,然后就是14 15 1 0注意一下就可以了. 代码如下: #pragma comment(linker, & ...