很简单的一道题,定义一个栈保留操作数,遇操作符则弹出运算即可。

bool isOperator(string &op)
{
//注意用法
return op.size() == && string("+-*/").find(op) != string::npos;
}
int evalRPN(vector<string> &tokens)
{
stack<string> s;
for (auto token : tokens)
{
if (!isOperator(token))
{
//如果是操作数,则入栈
s.push(token);
}
else
{
//如果是操作符,则弹出操作数进行运算
int y = stoi(s.top());
s.pop();
int x = stoi(s.top());
s.pop();
if (token == "+")x += y;
if (token == "-")x -= y;
if (token == "*")x *= y;
if (token == "/")x /= y;
s.push(to_string(x));
}
}
return stoi(s.top());
}

Leetcode 之Evaluate Reverse Polish Notation(41)的更多相关文章

  1. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  2. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  3. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

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

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

  5. 【leetcode】Evaluate Reverse Polish Notation(middle)

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

  6. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

  7. leetcode 150. Evaluate Reverse Polish Notation ------ java

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

  8. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

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

  9. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

随机推荐

  1. C#基础-连接Access与SQL Server

    1.连接Access数据库 string strConnection = "Provider=Microsoft.Ace.OleDb.12.0; Data Source=" + S ...

  2. 洛谷 P2757 [国家集训队]等差子序列 解题报告

    P2757 [国家集训队]等差子序列 题目描述 给一个\(1\)到\(N\)的排列\(\{A_i\}\),询问是否存在 \[1 \le p_1<p_2<p_3<p_4<p_5& ...

  3. git使用笔记(十二)stash

    By francis_hao    Oct 29,2017   git stash 保存当前工作目录的修改 概要 git stash list [<options>]git stash s ...

  4. bzoj 1111 [POI2007]四进制的天平Wag 数位Dp

    1111: [POI2007]四进制的天平Wag Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 302  Solved: 201[Submit][St ...

  5. ACM1198Farm Irrigation

    这个题目好吓人呀!嘿嘿--- 不过仔细分析下就可以啦! #include<iostream> #include<cstring> using namespace std; ; ...

  6. git设置不需要密码

    https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速 设置记住密码(默认15分钟): git config --global credenti ...

  7. hdu 4903 The only survival

    The only survival http://acm.hdu.edu.cn/showproblem.php?pid=4903 Time Limit: 40000/20000 MS (Java/Ot ...

  8. VM 脚本回快照和开关机

    #Import PowerCLI*Get-Module -ListAvailable PowerCLI* | Import-Module #Resolve login issueSet-PowerCL ...

  9. 【usaco-Liars and Truth Tellers, 2013 Jan真假奶牛】并查集

    题解: 原先我看错题了,以为是任意选择k个使得它们不矛盾. 这样的话怎么做呢?我想M^2判断,把它们分成若干个集合,集合里面两两不矛盾这个集合里所有的话就不矛盾了. 但是这样是错的.为什么呢? 每一句 ...

  10. 【BZOJ】1607: [Usaco2008 Dec]Patting Heads 轻拍牛头

    [算法]模拟 #include<cstdio> #include<algorithm> using namespace std; ,maxm=; int a[maxn],A[m ...