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. Java调用存储过程出现Bug,sql语法错误

    因为SQL Server运行没有正常,检查了传入参数的值,发现问题,然后传入默认参数,解决了问题.

  2. python之道05

    1.写代码,有如下列表,按照要求实现每一个功能 li = ["alex", "WuSir", "ritian", "barry&q ...

  3. ELK日志分析 学习笔记

    (贴一篇之前工作期间整理的elk学习笔记) ELK官网 https://www.elastic.co   ELK日志分析系统 学习笔记 概念:ELK = elasticsearch + logstas ...

  4. java常考小程序

    private static void nineNineMulitTable(){ /** * 9*9乘法表 */ for (int i = 1,j = 1; j <= 9; i++) { Sy ...

  5. tableview和searchbar的适配

    iOS7中,如果用UITableViewStyleGrouped的话,里面的 cell会比原来的拉长了,这样做应该是为了统一和UITableViewStylePlain风格时cell的大小一致,所以改 ...

  6. (11)zabbix item types监控类型

    1. 什么是item types item types是由zabbix提供的各种类型的检查器(这样翻译很奇怪),大致就是Zabbix agent, Simple checks, SNMP, Zabbi ...

  7. GIMP里的Path移动,旋转,翻转操作

    1/Path的移动: 快捷键Ctrl+Move Tool 2/Path的旋转: 选择Rotate Tool,在Path中选择,出现十字圈. Angel下的滑块调节一定的角度,在合适的位置即可. 3/P ...

  8. [php扩展] php安装扩展注意事项

    添加扩展的时候注意此3项 用的编译器版本:VC11... 安装的php版本:x86/x64 是否线程安全:enabled / disabled

  9. LeetCode(2)Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  10. ASP.NET MVC中如何在当前页面上弹出另外一个页面

    注意:不是链接到另一个页面,而是弹出一个页面,当前的页面和弹出页面都存在于浏览器的同一个标签页中,效果如图: 弹出的窗体置于四大天王页面之上,但是无法继续操作底层的页面,代码如下: function ...