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. 【Qt】2.1 创建对话框

    QDialog是Qt对话框类,可以直接使用这个类来创建对象并显示出来. 要使用一个对话框,就这样子写: #include <QApplication> #include <QDial ...

  2. vue父组件获取子组件页面的数组 以城市三级联动为例

    父组件调用子组件 <Cselect ref="registerAddress"></Cselect> import Cselect from '../../ ...

  3. hihoCoder-1093-SPFA

    SPFA的卓越之处就在于处理多点稀疏图,因为点太多的话,我们直接用矩阵来存图的话是存不下的. 所以当我们用邻接矩阵来存图的话,我们就可以用SPFA来解决这类问题,spfa就是优化版的bellman-f ...

  4. 破解点触码的识别之第三方平台超级鹰的SDK(python3版本)

    import requestsfrom hashlib import md5 class Chaojiying(object): def __init__(self, username, passwo ...

  5. IE6,7,8,9还有火狐浏览器的兼容

    /*FF.Opear等支持Web标准的浏览器*/#header {        margin-top: 23px;        margin-bottom: 23px;}/*IE6浏览器*/*ht ...

  6. java获取时间格式

    文章来源:https://www.cnblogs.com/hello-tl/p/9263602.html package com.util; import java.text.SimpleDateFo ...

  7. logging日志模块,四种方式

    1.最简单的用法 import logging logging.error("hah") logging.info("hah") logging.debug(& ...

  8. Python基础知识-day2

    格式化输出 %占位符,s字符串,d 数字, 表示%  用%% name = input("请输入姓名: ") age = input("请输入年龄: ") he ...

  9. Verilog学习笔记基本语法篇(一)·········数据类型

    Verilog中共有19种数据类型. 基本的四种类型: reg型.wire型.integer型.parameter型. 其他类型:large型.medium型.small型.scalared型.tim ...

  10. 利用Vert.x构建简单的API 服务、分布式服务

    目前已经使用Vertx已经一年多了,虽然没有太多的造诣,但也已在项目中推广了下:从最初的vertx搭建web服务,到项目上线运营,还算比较稳定.再到后来尝试搭建基于vertx的分布式服务,一路下来也积 ...