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. PAT (Basic Level) Practice 1006 换个格式输出整数

    个人练习 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(&lt10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为 ...

  2. C++基础 inline 默认参数 函数占位参数 函数重载

    1. inline内联函数 内联函数用于替换宏, 实例: 其中宏和 ++ 连用有副作用. #include "iostream" using namespace std; #def ...

  3. [Bzoj4408]神秘数(主席树)

    Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数. 例如S={1,1,1,4,13}, 1 = 1 2 = 1+1 3 = 1+1+1 4 = 4 5 = ...

  4. urllib使用一

    urllib.urlopen()方法: 参数: 1.url(要访问的网页链接http:或者是本地文件file:) 2.data(如果有,就会由GET方法变为POST方法,提交的数据格式必须是appli ...

  5. 【数据库】MySQL 从安装到命令

    一, MySQL 的安装于配置 我是通过百度云盘的方式下载的.建议登录百度云终端,然后点击下面的链接,选择要安装的版本,解压安装. http://www.h2ero.cn/pan/share/17cd ...

  6. Android Html处理器通用类 HtmlUtil

    1.整体分析 1.1.首先看一下源代码,可以直接Copy. public class HtmlUtil { /** * 获取 html 中的纯文本 */ public static String Ht ...

  7. 初学JS——实现基于计时器的小游戏。

    这几天一直在看网易云课堂上免费的JS课程,正好今天看到讲了计时器setInterval,第一感觉就是像C#里的TIMER.然后课程里举得例子正好通过计时器改变新生成窗口的位置, 然后就突然有了灵感!可 ...

  8. 剑指Offer - 九度1390 - 矩形覆盖

    剑指Offer - 九度1390 - 矩形覆盖2014-02-05 23:27 题目描述: 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形 ...

  9. Python——数据类型之list、tuple

    本篇主要内容 •  list初识 •  list元素的访问 •  list内部所有的方法 •  tuple介绍和与list用法的比较 我觉得Python里面用的最多的就是List了,感觉好强大.他能存 ...

  10. React01补充

    使用yarn安装脚手架 npm i -g yarn npm uninstall -g create-react-app yarn global add create-react-app create- ...