【LeetCode】150. Evaluate Reverse Polish Notation
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
使用栈(Stack),如果是操作数则压栈,如果是操作符则弹出栈顶两个元素进行相应运算之后再压栈。
最后栈中剩余元素即计算结果。
注意:操作数的顺序。
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> stk;
for(int i = ; i < tokens.size(); i ++)
{
if(tokens[i] == "+")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b+a);
}
else if(tokens[i] == "-")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b-a);
}
else if(tokens[i] == "*")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b*a);
}
else if(tokens[i] == "/")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b/a);
}
else
{
int digit = atoi(tokens[i].c_str());
stk.push(digit);
}
}
return stk.top();
}
};
【LeetCode】150. Evaluate Reverse Polish Notation的更多相关文章
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode OJ 150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 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 ------ java
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 +, -, ...
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
随机推荐
- 用wubi安装的Ubuntu在重装Windows 7系统后,如何恢复(转)
原文链接:双系统Win7+Ubuntu,重装Win7后找不到Ubuntu启动引导项问题 1.把安装ubuntu->winboot文件夹下wubidr和wubidr.mbr两个文件拷到C盘根目录下 ...
- design-twitter
https://leetcode.com/problems/design-twitter/ class Twitter { unordered_map<int, set<int> & ...
- C#操作AD及Exchange Server总结(一)
这篇博客的目的:根据亲身项目经历,总结对AD及Exchange Server的操作,包括新建AD用户,设置密码,为AD用户创建邮箱等. 本文完全原创,转载请说明出处,希望对大家有用. 文档目录: 测试 ...
- 关于opacity的思考
今天在封装图片轮播的插件的时候,产生了这个opacity的小小思考. 我这个轮播的思路不是以前baidu输入法官网的设置外层容器overflow为hidden,position为relative用se ...
- Andorid之ActivityManager
在Android中ActivityManager主要用于和系统中运行的Activities进行交互.在本篇文章中,我们将对ActivityManager中的API进行研究使用. 在ActivityMa ...
- 如何使用Neofetch个性化显示Linux系统信息
可用于查看和显示 Linux 系统信息的开源工具和脚本实在太多,Neofetch 也是其中之一,Neofetch 可以以更全面的方式来显示输出详实的 Linux 系统信息,简单地来说,如果你想查看 L ...
- Windows 抓取本地环路包
1.cmd,ipconfig查看自己的ip地址. 2.执行命令:route add 10.36.65.89 mask 255.255.255.255 10.36.65.1 metric 1,其中10. ...
- Discuz常见小问题-如何快速安装和配置
下载PHPNOW 可以解压到本地的某个目录,最好不要有中文路径,然后查看Readme进行安装,双击Setup.cmd 安装结束之后,会要求输入一个初始化的密码,不要忘记,会自动弹出一个测试页面,可以测 ...
- 读取Style符号库样式的方法
以前进行符化的时候一般都是自定义Symbol,或者使用SymbologyControl进行选择,由于实际需要,我们来读取一下样式管理器中的样式.在ArcMap中打开如下:style下有很多样式类,每个 ...
- Loadrunner 11 遇到的问题
环境 OS:windows 8.1 64bit LoadRunner版本:11 问题 1. VuGen:开始录制后,火狐浏览器没有反应,不会弹出打开 可能原因一:浏览器版本太高. 解决方案: 1)卸 ...