算法描述:

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

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

Note:

  • Division between two integers should truncate toward zero.
  • The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.

Example 1:

Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Example 3:

Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

解题思路:逆波特兰表达式,用栈辅助模拟。

    int evalRPN(vector<string>& tokens) {if(tokens.size()==0) return 0;stack<int> stk;for(auto c : tokens){if(c == "+" || c == "-" || c== "*" || c=="/"){int right = stk.top();stk.pop();int left = stk.top();stk.pop();int ans;if(c == "+") ans = left + right;if(c == "-") ans = left - right;if(c == "*") ans = left * right;if(c == "/") ans = left / right;stk.push(ans);}else{stk.push(stoi(c));}}return stk.top();}

转载于:https://www.cnblogs.com/nobodywang/p/10354128.html

LeetCode-150-Evaluate Reverse Polish Notation相关推荐

  1. LeetCode 150. Evaluate Reverse Polish Notation

    LeetCode 150. Evaluate Reverse Polish Notation Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/42 ...

  2. leetcode - 150. Evaluate Reverse Polish Notation

    前言:记录一下leetcode上的一道题目: 题目网址:https://leetcode.com/problems/evaluate-reverse-polish-notation/descripti ...

  3. Leetcode 之Evaluate Reverse Polish Notation(41)

    很简单的一道题,定义一个栈保留操作数,遇操作符则弹出运算即可. bool isOperator(string &op){//注意用法return op.size() == 1 &&am ...

  4. leetcode day1 -- Reverse Words in a String Evaluate Reverse Polish Notation Max Points on a Li

    以前从来没做过什么oj,发现做oj和在本地写代码或者纸上写差别还是很大的,觉得今天开始刷oj,特此记录一下. 1.Reverse Words in a String Given an input st ...

  5. 计算后续表达式 LectCode之Evaluate Reverse Polish Notation

    在记录lectcode这道题目前先说明一下三个相关知识点:前序表达式,中序表达式,后序表达式 前序表达式(Polish Notation 或 Prefix Notation): 前序表达式就是不含括号 ...

  6. 150 Evaluate Reverse Polish

    1题目理解 输入:一个字符串数组.这个字符串数组表示算数运算的逆波兰表示法.一般算数表示方法是2+1,逆波兰表示是2 1 +. 输出:一个int值. Example 1: Input: [" ...

  7. Algorithm:C++语言实现之链表相关算法(单链公共结点问题、一般LCA、括号匹配、最长括号匹配、逆波兰表达式Reverse Polish Notation、直方图矩形面积、收集雨水问题)

    Algorithm:C++语言实现之链表相关算法(单链公共结点问题.一般LCA.括号匹配.最长括号匹配.逆波兰表达式Reverse Polish Notation.直方图矩形面积.收集雨水问题) 目录 ...

  8. Reverse Polish Notation

    http://www.1point3acres.com/bbs/thread-31595-1-1.html 定义一种叫做"Reverse Polish Notation"的表达式: ...

  9. [LeetCode]题解(python):150-Evaluate Reverse Polish Notation

    题目来源: https://leetcode.com/problems/evaluate-reverse-polish-notation/ 题意分析: 给定一个数组,用这个数组来表示加减乘除,例如 [ ...

最新文章

  1. Eclipse启动之四 : Eclipse核心框架启动(百度空间迁移)
  2. Windows 7防火墙设置详解(三)
  3. 台湾大学林轩田机器学习技法课程学习笔记12 -- Neural Network
  4. android调试神器Stetho
  5. 【今日CV 视觉论文速览】15 Nov 2018
  6. Java多线程相关的几十个问题
  7. 基于python开发的口罩供需平台
  8. 边缘设备上的实时AI人员检测:以实时模式检测视频中的人员
  9. 打开 .npy文件 并显示
  10. ps6人脸识别液化工具在哪_Photoshop教学:人脸识别液化功能介绍
  11. 企业微信开发实战(二、OA审批之获取审批模版详情提交审批申请)
  12. 华为实验-关于不同vlan之间的互通 混合实验
  13. ads1278_ADS1278
  14. 简单说说我是怎么找回U盘删除的文件
  15. PostMessage,SendMessage,GetMessage,PeekMessage,TranslateMessage,DispatchMessage的用法集合
  16. el-upload回显细节--没有图片数据返回的时候每点击一次添加多了一个空白图片
  17. VS2022(Visual Studio)发布ASP.NET Core Web API应用到Web服务器(IIS)
  18. mysql error code 100_Mysql 导入报错 error code:1166
  19. Python 分别获取日期中的年月日时分秒
  20. 探索在原生网页中使用自定义数据属性

热门文章

  1. QTP工作原理的学习心得
  2. 16-margin的用法
  3. -bash-4.1问题
  4. [转载]了解Linux的进程与线程
  5. RabbitMq--AMQP高级消息队列协议--简单了解
  6. [导入]Spring Web Flow
  7. Jedis与Redisson对比有什么优缺点?
  8. 浅谈linux IO
  9. Docker持续交付部署类型
  10. akka actor行为切换实现