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的更多相关文章

  1. Leetcode OJ : Evaluate Reverse Polish Notation Stack C++ solution

    #define ADDITION '+' #define SUBSTRACTION '-' #define MULTIPLICATION '*' #define DIVISION '/' class ...

  2. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  3. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  4. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

  5. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  6. 【leetcode】Evaluate Reverse Polish Notation(middle)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  7. Java for LeetCode 150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. leetcode 150. Evaluate Reverse Polish Notation ------ java

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  10. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

随机推荐

  1. 【单片机实验】6LED静态串行显示

    实验三 6LED静态串行显示一.实验目的1.掌握数字.字符转换成由数码管显示的八段码的软件译码方法及译码过程:2.静态显示的原理和相关程序的编写. 二.实验电路静态显示 电路如图3-2所示.显示器由6 ...

  2. C02 信息存储与运算

    目录 计算机内存 常量和变量 数据类型 运算符 计算机内存管理 计算机内存 信息存储概述 使用程序进行开发时,需要存储各种信息,这时候就需要用到变量.由于信息类型不同,变量的类型也因此不尽相同. 同时 ...

  3. Sniper OJ部分writeup

    0x00 shellcode pwn 因为题目直接有源码,我就不拖进IDA了,直接看代码 这是一个典型的栈溢出,我们只需要构造shellcode获得/bin/sh权限就可以得到flag.下面是所用脚本 ...

  4. C#获得DataTable的key值

    //获得dataTable的key值 List<string> keyList = new List<string>(); ; i < dt.Columns.Count; ...

  5. PAT 乙级 1037

    题目 题目地址:PAT 乙级 1037 题解 本题有两个版本的代码,初版因为种种问题写得比较繁琐,具体的分析见后文,更新的之后的版本相对来说要好很多,代码也比较清晰简洁. 初版的代码主要有如下几方面的 ...

  6. 自定义loader基础知识

    参考 :译文 编写一个loader https://webpack.github.io/docs/loaders.html 按照loader的返回值可以分为两种: 最左loader:这种loader会 ...

  7. 初中级PHP面试基础汇总

    这是我整理的一套面试题,老铁们看看就当复习了哦 相关PHP面试题 搞定PHP面试 - 函数知识点整理 php 面试题目整理 PHP面试整理 PHP面试 概述 感觉现在发面试题有些冷门,就跟昨天德国那场 ...

  8. MySQL之单表查询、多表查询

    一.单表查询: 单个表的查询方法及语法顺序需要通过实际例子来熟悉 先将表数据创建下: mysql> create database singe_t1; # 建个数据库singe_t1 Query ...

  9. (转)iOS 最佳实践

    本文转自http://www.jianshu.com/p/b0bf2368fb95 感谢作者和译者 iOS最佳实践 iOS最佳实践 译者注 本文翻译自 futurice 公司的 iOS Good Pr ...

  10. Oracle中Restore和Recovery的区别

    一.参考解释一 在Oracle的备份与恢复的知识点中,经常会出现Restore 和 Recovery两个词. 由于这两个词在字典中的解释很接近,困扰了我很久.直到我在Oracle的官方文档中看到了以下 ...