LeetCode OJ--Evaluate Reverse Polish Notation
http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
栈使用
#include <iostream>
#include <string>
#include <vector>
#include<stack>
using namespace std; class Solution {
public:
int toNum(string str)
{
int sum = ;
int i = ;
int flagPositiveOrNegative = ;;
if(str[] == '-')
{
flagPositiveOrNegative = -;
i = ;
}
for( i;i<str.size();i++)
{
sum *= ;
sum += str[i] - '';
}
return sum * flagPositiveOrNegative;
}
int evalRPN(vector<string> &tokens) {
if(tokens.size()==)
return ;
int i = ;
stack<int> myStack;
while(i<tokens.size())
{
if(tokens[i] == "+" || tokens[i] == "-" || tokens[i]=="*" ||tokens[i] == "/" )
{
int num1 = myStack.top();
myStack.pop();
int num2 = myStack.top();
myStack.pop();
if(tokens[i] == "+")
myStack.push(num1+num2);
if(tokens[i] == "-")
myStack.push(num2-num1);
if(tokens[i] == "*")
myStack.push(num1*num2);
if(tokens[i] == "/")
myStack.push(num2/num1);
}
else
{
myStack.push(toNum(tokens[i]));
}
i++;
}
return myStack.top();
}
}; int main()
{
Solution myS;
vector<string> input;
input.push_back("");
input.push_back("-4");
input.push_back("+");
cout<<myS.evalRPN(input);
return ;
}
LeetCode OJ--Evaluate Reverse Polish Notation的更多相关文章
- Leetcode OJ : Evaluate Reverse Polish Notation Stack C++ solution
#define ADDITION '+' #define SUBSTRACTION '-' #define MULTIPLICATION '*' #define DIVISION '/' class ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- [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(middle)
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 +, -, ...
- LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
随机推荐
- 79 最长公共子串 (lintcode)
f[i][j]表示的是以第i个结尾和第j个结尾 class Solution { public: /* * @param A: A string * @param B: A string * @ret ...
- 爬虫学习之pdf读取和存储
在py3中如需进行pdf文件操作需要加载PDFMiner3K库文件,可通过pip方式或者可以下载源文件方式安装 python3 -m pip install pdfminer3k 下载源文件方式: 1 ...
- 【树形dp】7.14城市
很典型的按照边考虑贡献的题. 题目描述 小A居住的城市可以认为由n个街区组成.街区从1到n依次标号街区与街区之间由街道相连,每个街区都可以通过若干条街道到达任意一个街区,共有n-1条街道.其中标号为i ...
- Linux基础学习-chrony时间同步服务
Chrony时间同步 NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议.它的用于是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可 ...
- 【git】不检查特定文件的更改情况
.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的.正确的做法是在每个clone下来的仓库中手动设置不要检查特定文件的更 ...
- Linux配置使用SSH Key登录并禁用root密码登录(替换同理)
Linux系统大多说都支持OpenSSH,生成公钥.私钥的最好用ssh-keygen命令,如果用putty自带的PUTTYGEN.EXE生成会不兼容OpenSSH,从而会导致登录时出现server r ...
- Python之写入文件(1)
一.写入文件 保存数据也是在各个编程语言中常用的操作,将数据写入到文件中是常用的操作,你可以将程序运行中的一些临时输出保存至文件中,以便后续打开文件查看,也可以把这些文件读入程序中来操作其中的数据. ...
- Python多版本共存安装
Python的安装 进入Python官方网站:www.python.org下载系统对应的Python版本 按照提示步奏安装,安装路径选择自定义,方便查找 安装完成后,按win+R键,输入cmd进入cm ...
- [转]构建Python+Selenium2自动化测试环境(二)
构建Python+Selenium2自动化测试环境完成之后,就需要测试支持python的selenium的版本是否都支持在不同浏览器上运行,当前我们分别在三个最通用的浏览器上通过脚本来测试. 1.在I ...
- x86保护模式 二 分段管理机制
分段管理机制 段选择子和偏移地址的二维虚拟地址转换为一维的线性地址 一 段定义和虚拟地址到线性地址的转换 三个参数定义段:段基地址 段界限 和段属性 同时也是段描述符的结构 段基地址为 ...