力扣算法题—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 Solution:
使用栈即可
class Solution {
public:
int evalRPN(vector<string> &tokens) {
if (tokens.size() == )return ;
stack<int>s;
for (auto a : tokens)
{
if (a == "+" || a == "-" || a == "*" || a == "/")
{
int num2 = s.top();
s.pop();
int num1 = s.top();
s.pop();
int res = ;
if (a == "+")
res = num1 + num2;
else if (a == "-")
res = num1 - num2;
else if (a == "*")
res = num1 * num2;
else
res = num1 / num2;
s.push(res);
}
else
s.push(atoi(a.c_str()));
}
return s.top();
}
};
力扣算法题—150. Evaluate Reverse Polish Notation的更多相关文章
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
- LeetCode OJ 150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- 转-C++之string判断字符串是否包含某个子串
转自:https://blog.csdn.net/zhouxinxin0202/article/details/77862615/ 1.string类函数find C++的string类提供了字符串中 ...
- PHP面试 linux基础
Linux基础 Linux常用命令 系统安全:sudo su chmod setfacl 进程管理:w top ps kill pkill pstree killall 用户管理 ...
- HDU6655 Just Repeat(2019杭电多校J题)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=6655 简单博弈问题,A,B手里各有n,m张牌,牌有颜色,两人轮流出牌(A先出),一个人只能打出对放未打 ...
- 安装Treserocr遇到的问题
相关链接: tesseract下载地址:http://digi.bib.uni-mannheim.de/tesseract 一.出现的问题 1.点击进去 进行下载 注意:其中文件名中带有dev的为开发 ...
- Python删除列表中的空格
list1 = ['122','2333','3444',' ','422',' ',' ','54',' '] list1=[x.strip() for x in list1 if x.strip( ...
- Python之向函数传递元组和字典
也可以在函数定义时加上这两个参数用以接收多余的参数哦~
- idea maven springmvc mybabit 多模块管理整合
一.安装软件jdk1.7,tomcat7.0,idea,mysql,maven 二.在idea中配置jdk 1.依次点开File -->Project Structure,点击左侧标签页,点击S ...
- JS window对象 screen对象 screen对象用于获取用户的屏幕信息。 语法: window.screen.属性
screen对象 screen对象用于获取用户的屏幕信息. 语法: window.screen.属性 对象属性:
- gradle笔记
gradle笔记 一.基础知识 1.1.groovy语言 Groovy适用于Java虚拟机的一种敏捷的动态语言,他是一种成熟的面向对象编程语言,既可以用于面向对象编程,又可以用作纯粹的脚本语言,使用该 ...
- js取整 - 优雅版(装逼必备)
var a = 2.98; var z1 = ~~a; var z2 = a | 0; var z3 = a>>0; console.log(z1, z2, z3); // 2, ...