LeetCode-150-Evaluate Reverse Polish Notation
算法描述:
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相关推荐
- LeetCode 150. Evaluate Reverse Polish Notation
LeetCode 150. Evaluate Reverse Polish Notation Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/42 ...
- leetcode - 150. Evaluate Reverse Polish Notation
前言:记录一下leetcode上的一道题目: 题目网址:https://leetcode.com/problems/evaluate-reverse-polish-notation/descripti ...
- Leetcode 之Evaluate Reverse Polish Notation(41)
很简单的一道题,定义一个栈保留操作数,遇操作符则弹出运算即可. bool isOperator(string &op){//注意用法return op.size() == 1 &&am ...
- 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 ...
- 计算后续表达式 LectCode之Evaluate Reverse Polish Notation
在记录lectcode这道题目前先说明一下三个相关知识点:前序表达式,中序表达式,后序表达式 前序表达式(Polish Notation 或 Prefix Notation): 前序表达式就是不含括号 ...
- 150 Evaluate Reverse Polish
1题目理解 输入:一个字符串数组.这个字符串数组表示算数运算的逆波兰表示法.一般算数表示方法是2+1,逆波兰表示是2 1 +. 输出:一个int值. Example 1: Input: [" ...
- Algorithm:C++语言实现之链表相关算法(单链公共结点问题、一般LCA、括号匹配、最长括号匹配、逆波兰表达式Reverse Polish Notation、直方图矩形面积、收集雨水问题)
Algorithm:C++语言实现之链表相关算法(单链公共结点问题.一般LCA.括号匹配.最长括号匹配.逆波兰表达式Reverse Polish Notation.直方图矩形面积.收集雨水问题) 目录 ...
- Reverse Polish Notation
http://www.1point3acres.com/bbs/thread-31595-1-1.html 定义一种叫做"Reverse Polish Notation"的表达式: ...
- [LeetCode]题解(python):150-Evaluate Reverse Polish Notation
题目来源: https://leetcode.com/problems/evaluate-reverse-polish-notation/ 题意分析: 给定一个数组,用这个数组来表示加减乘除,例如 [ ...
最新文章
- Eclipse启动之四 : Eclipse核心框架启动(百度空间迁移)
- Windows 7防火墙设置详解(三)
- 台湾大学林轩田机器学习技法课程学习笔记12 -- Neural Network
- android调试神器Stetho
- 【今日CV 视觉论文速览】15 Nov 2018
- Java多线程相关的几十个问题
- 基于python开发的口罩供需平台
- 边缘设备上的实时AI人员检测:以实时模式检测视频中的人员
- 打开 .npy文件 并显示
- ps6人脸识别液化工具在哪_Photoshop教学:人脸识别液化功能介绍
- 企业微信开发实战(二、OA审批之获取审批模版详情提交审批申请)
- 华为实验-关于不同vlan之间的互通 混合实验
- ads1278_ADS1278
- 简单说说我是怎么找回U盘删除的文件
- PostMessage,SendMessage,GetMessage,PeekMessage,TranslateMessage,DispatchMessage的用法集合
- el-upload回显细节--没有图片数据返回的时候每点击一次添加多了一个空白图片
- VS2022(Visual Studio)发布ASP.NET Core Web API应用到Web服务器(IIS)
- mysql error code 100_Mysql 导入报错 error code:1166
- Python 分别获取日期中的年月日时分秒
- 探索在原生网页中使用自定义数据属性