Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +-*/. Each operand may be an integer or another expression.

Some examples:

  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
Hide Tags

Stack

 

    题目是用 stack 维护一个公式的进出,判断方法还可以,开始忘记数可能为负,后面改进了。
 
#include <stack>
#include <iostream>
#include <string>
#include <vector>
using namespace std; class Solution {
public:
int evalRPN(vector<string> &tokens) {
int n = tokens.size();
stack<int > tmp;
for(int i=;i<n;i++){
if(tokens[i][]>=''&&tokens[i][]<=''){
tmp.push( helpFun(tokens[i]) );
continue;
}
if(tokens[i][]=='-'&&tokens[i][]!='\0'){
tmp.push( helpFun( tokens[i]));
continue;
}
int rgt = tmp.top();
tmp.pop();
int lft = tmp.top();
tmp.pop();
switch (tokens[i][]){
case '+':
tmp.push( lft + rgt );
break;
case '-':
tmp.push(lft - rgt);
break;
case '*':
tmp.push(lft * rgt);
break;
case '/':
tmp.push(lft / rgt);
break;
}
}
return tmp.top();
} int helpFun(string str)
{
int sum = ,i = ;
if (str[]=='-')
i = ;
for(;i<str.length();i++)
sum = sum*+str[i]-'';
return str[]=='-'?-sum:sum;
}
}; int main()
{
vector<string> tokens{"","-4","+"};
Solution sol;
cout<<sol.evalRPN(tokens)<<endl;
// for(int i=0;i<tokens.size();i++)
// cout<<tokens[i]<<endl;
return ;
}

[LeetCode] Evaluate Reverse Polish Notation stack 栈的更多相关文章

  1. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

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

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

  3. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  4. [leetcode]Evaluate Reverse Polish Notation @ Python

    原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...

  5. [LeetCode] Evaluate Reverse Polish Notation [2]

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

  6. Leetcode Evaluate Reverse Polish Notation

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

  7. leetcode——Evaluate Reverse Polish Notation 求算式值(AC)

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

  8. 150. Evaluate Reverse Polish Notation (Stack)

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

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

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

随机推荐

  1. python中的文件操作小结2

    ''' #-----------文件修改---------- f=open("test_1",'r',encoding="utf-8") f2=open(&qu ...

  2. 使用shell脚本依据分区信息分批次的下载hive表格数据

    今天的业务场景大概是这样的,我想把hive表格下载到本地文件系统,然后把这个文件传送到另一个服务器上. 但是这个业务场景一个核心问题就是说我本地机器内存有限,hive表格大概是70G,我是不可能全部下 ...

  3. 006---Django静态文件配置

    静态文件:Js.Css.Fonts.Image等 这个不难.在setting.py文件加一行 # 别名 用户在url地址栏输入127.0.0.1:8000/static/文件 可以直接访问static ...

  4. POJ:3040-Allowance(贪心好题)

    Allowance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4903 Accepted: 1943 Description ...

  5. Flask错误收集 【转】

    感谢大佬 ---> 原文链接 一.pydev debugger: process XXXXX is connecting 这个错误网上找了很多资料都无法解决,尝试过多种方法后,对我来说,下面这个 ...

  6. hdu1251统计难题(trie)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  7. PAT、PMT、SDT详解

    下面针对解复用程序详细分析一下PAT,PMT和SDT三类表格的格式. 如下图,四个频道复用 PAT---Program Association Table,节目关联表 .PAT表携带以下信息: (1) ...

  8. laravel5.5开发composer扩展包

    目录 1. 下载laravel框架,并命名(framework) 2. 创建相关目录 3. 项目根目录下的composer.json文件中声明命名空间 4. 在包的根目录(packages/arche ...

  9. Xcode 代码提示功能失效

    前言: 以前好像很少碰到Xcode中代码提示出问题的情况,最近经常遇到这个问题.没有了Xcode的智能提示,发现我已完全不会写代码了. 本来想吐槽下万恶的baidu,鉴于百度前端时间的各种(贴吧.竞价 ...

  10. Python 3基础教程9-函数

    本文介绍Python中的函数,主要了解如何定义一个函数,如何调用一个函数. 如果上面你不写调用函数这行代码,你运行后,是没有打印输出的.我们这里来,结合前面的if语句来定义一个,两个数比较,判断最大的 ...