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/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
随机推荐
- hash 散列表
一个字符串的hash值: •现在我们希望找到一个hash函数,使得每一个字符串都能够映射到一个整数上 •比如hash[i]=(hash[i-1]*p+idx(s[i]))%mod •字符串:abc,b ...
- Hopfield 网络(上)
讲的什么 这部分主要对 Hopfield 网络作一大概的介绍.写了其模型结构.能量函数和网络的动作方式.主要参考了网上搜到的一些相关 PPT. 概述 早在 1982 年,Hopfield 发表的文 ...
- html中常见符号的代码表示
HTML中空格的集中代码表示: HTML中空格 不断行的空白(1个字符宽度) 半个空白(1个字符宽度) 一个空白(2个字符宽度) 窄空白(小于1个字符宽度) 其他常见的 ...
- Bootstrap 网格系统(Grid System)实例3
Bootstrap 网格系统(Grid System)实例:堆叠水平 <!DOCTYPE html><html><head><meta http-equiv= ...
- ThinkPHP项目怎么运行?
1.下载ThinkPHP项目 2.安装核心框架framework 3.配置集成开发环境:wamp或者xampp或者phpStudy
- $monitor用法
1.$monitor 进程同一时间有且仅有一个,若多次调用$monitor,新进程会代替以前的monitor进程. 2.$fmonitor可以同时存在任意个. 3.一般不用$monitor系统函数. ...
- [php] 高级教程
include 和 require 语句用于在执行流中插入写在其他文件中的有用的代码. include 和 require 除了处理错误的方式不同之外,在其他方面都是相同的: require 生成一个 ...
- c++ 快速幂 代码实现
懒得打代码系列… 不过这个代码挺短的死背下来也ok 解析在最下面 建议自己手动试个数据理解一下 比如 3^5 ^^ 原理:a ^ b = a ^ (b / 2) * 2 (b是奇数的话还要再乘一个a) ...
- DRF filter
filter 配置 fiter定义 自定义filter继承BaseFilterBackend,必须重写filter_queryset,返回值为过滤后的queryset filter在GenericAP ...
- VS2010SP1修复补丁&Microsoft Visual Studio 2010 Service Pack 1
网上比较难找,官网找也容易找错,现在贴出来 补丁包下载地址:链接:https://pan.baidu.com/s/1_tFzXL6PaHiWk3JeRBw0ww 密码:z38k