题目

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

分析

本题考查的是栈的应用,计算后缀表达式的值。

参考数据结构,栈章节。

AC代码

class Solution {
public:
int evalRPN(vector<string>& tokens) {
if (tokens.empty())
return 0; //存储运算数
stack<int> s; //tokens容量
int size = tokens.size();
for (int i = 0; i < size; ++i)
{
if (!isOper(tokens[i]))
{
s.push(strToInt(tokens[i]));
}
else{
char op = tokens[i][0];
switch (op)
{
int op1, op2;
case '+':
op1 = s.top();
s.pop();
op2 = s.top();
s.pop();
s.push(op2 + op1);
break;
case '-':
op1 = s.top();
s.pop();
op2 = s.top();
s.pop();
s.push(op2 - op1);
break;
case '*':
op1 = s.top();
s.pop();
op2 = s.top();
s.pop();
s.push(op2 * op1);
break;
case '/':
op1 = s.top();
s.pop();
op2 = s.top();
s.pop();
s.push(op2 / op1);
break;
default:
break;
}//switch
}//else
}//for
return s.top(); } //判断是否为运算符
bool isOper(string &str)
{
if (str.size() > 1)
return false; if (str[0] == '+' || str[0] == '-' || str[0] == '*' || str[0] == '/')
return true;
return false;
} //将字符串转换为整数
int strToInt(string &str)
{
if (str.empty())
return 0; // 求字符串长度
int size = str.size(); int flag = 1, pos = 0, sum = 0, multi = 1;
if (str[0] == '-')
{
flag = -1;
pos = 1;
} for (int i = size - 1; i >= pos; --i)
{
sum += (str[i] - '0') * multi;
multi *= 10;
} return flag * sum;
}
};

GitHub测试程序源码

LeetCode(150) Evaluate Reverse Polish Notation的更多相关文章

  1. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

  3. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  4. 【LeetCode】150. Evaluate Reverse Polish Notation

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

  5. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

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

  6. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  7. 【leetcode】Evaluate Reverse Polish Notation

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

  8. 【LeetCode练习题】Evaluate Reverse Polish Notation

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

  9. leetcode - [2]Evaluate Reverse Polish Notation

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

随机推荐

  1. scrapy框架中Spiders用法

    scrapy框架中Spiders用法 Spider类定义了如何爬去某个网站,包括爬取的动作以及如何从网页内容中提取结构化的数据 总的来说spider就是定义爬取的动作以及分析某个网页 工作流程分析 以 ...

  2. Qt 2D绘图之二:抗锯齿渲染和坐标系统

    一.抗锯齿渲染 1.1 逻辑绘图 图形基元的大小(宽度和高度)始终与其数学模型相对应,下图示意了忽略其渲染时使用的画笔的宽度的样子. 1.2 物理绘图(默认情况) 在默认的情况下,绘制会产生锯齿,并且 ...

  3. CSS入门使用

    声明标签 HTML <!DOCTYPE> 内链样式表 <body style="background-color:green;margin:0;padding:0;&quo ...

  4. simhash与重复信息识别

    在工作学习中,我往往感叹数学奇迹般的解决一些貌似不可能完成的任务,并且十分希望将这种喜悦分享给大家,就好比说:“老婆,出来看上帝”…… 随着信息爆炸时代的来临,互联网上充斥着着大量的近重复信息,有效地 ...

  5. 09通过winfrom实现简单的播放音、视频

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. net core 在docker(ubuntu)部署

    1.vs新建项目并发布,然后copy到linux系统上,我这里是用的虚拟机. 2 Dockerfile文件配置 FROM microsoft/dotnet:2.1-aspnetcore-runtime ...

  7. Java之final、static关键字及匿名对象

    个人通俗理解: 1.final:首先被final修饰的变量就自动变成的不能被修改的常量了.被修饰的类会自动变成太监类,只能有父类,不能有子类:被修饰的方法也不能被子类重写了:被修饰的引用变量值也不能更 ...

  8. 一张图告诉你,只会这些HTML还远远不够!!!!!

    不知道自己HTML水平如何,不知道HTML5如何进化?看这张图 如果一半以上的你都不会,必须看这本书,阿里一线工程师用代码和功能页面来告诉你每一个技术点. 都会一点,但不知道如何检验自己,看看本书提供 ...

  9. JQ中的问题

    $(function(){$(document).bind("click", function (e) {$(e.target).closest("p").cs ...

  10. iOS NSDate 常用日期相关函数的封装

    Category是类别,一般情况用分类好,用Category去重写类的方法,仅对本Category有效,不会影响到其他类与原有类的关系. NSDate+Category.h 代码: #import & ...