leetcode150 Evaluate Reverse Polish Notation
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
计算逆波兰表达式,使用一个栈来存储操作数字,遍历输入的链表。
如果为数字,直接push到栈内,如果为操作符号,就把栈顶的两个元素pop出来运算一下。
运算的结果push到栈内。
public class Solution {
public int evalRPN(String[] tokens) {
int result = 0;
Stack<Integer> stack = new Stack<Integer>();
String operators = "+-*/"; for(int i=0; i<tokens.length;i++){
if (operators.contains(tokens[i])){
int b = stack.pop();
int a = stack.pop();
stack.push(calculate(a,b,tokens[i]));
}else{
stack.push(Integer.valueOf(tokens[i]));
}
}
result = stack.pop();
return result;
} public int calculate(int a, int b, String operator){
char op = operator.charAt(0);
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}
return 0;
}
}
leetcode150 Evaluate Reverse Polish Notation的更多相关文章
- Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
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 ...
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
随机推荐
- vs------密钥
HM6NR-QXX7C-DFW2Y-8B82K-WTYJV
- Win10微软官方最终正式版ISO镜像文件
Win10微软官方最终正式版ISO镜像文件 据说Windows 10是微软发布的最后一个Windows版本,下一代Windows将作为Update形式出现.Windows 10将发布7个发行版本,分别 ...
- easyUI文本框textbox笔记
知识点: 1.图标位置 Icon Align属性,有left和right两个: 2.textbox的setvalue方法,getvalue方法. <div style="margin: ...
- vim 快捷键 以及技巧
[root@centos01 biji]# vim + 1.txt 打开文件,光标定位到最后一行[root@centos01 biji]# vim +5 1.txt 打开文件,光标定位到第5行[roo ...
- winSocket数据传输
服务器端: #include <WINSOCK2.H> #include <stdio.h> #pragma comment(lib,"ws2_32.lib" ...
- 提交表单注意事项<script>11111</script>
<input name="name" value="" /> 如果在上面表单中 ,填写 <script>alert('111')< ...
- MVC5的AuthorizeAttribute详解
现今大多数的网站尤其是购物网站都要求你登录后才能继续操作,当你匿名的将商品放入购物车后,不可能匿名购买这时可以转到登录界面让用户进行登录验证. 适用系统自带的过滤器 MVC5只要将属性[Authori ...
- Python之路【第七篇】:初识Socket
What is Socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制, ...
- idea配置2个tomcat
复制tomcat 分别放在不同地方
- charles使用教程指南
文章转自:http://drops.wooyun.org/tips/2423 安装charles 下载路径:http://www.charlesproxy.com/download/ 如果是ubunt ...