LeetCode-EvaluteReversePolishNotation
题目:
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
分析:
求解逆波兰表达式,从公式中提取字符串,如果是操作数就push进栈中,如果是操作符就从栈中pop出两个数,用第二个数和第一个数做运算.
AC代码:
//求解逆波兰表达式
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> num;
for(auto token:tokens){
if(token == "+" || token == "-" || token == "*"||token == "/"){
int a, b, ans;
b = num.top(); num.pop();
a = num.top(); num.pop();
if(token == "+")
ans = a + b;
if(token == "-")
ans = a - b;
if(token == "*")
ans = a * b;
if(token == "/")
ans = a / b;
num.push(ans);
}
else{
num.push(stoi(token));
}
}
return num.top();
}
};
LeetCode-EvaluteReversePolishNotation的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- 如何在mysql中查询每个分组的前几名
问题 在工作中常会遇到将数据分组排序的问题,如在考试成绩中,找出每个班级的前五名等. 在orcale等数据库中可以使用partition 语句来解决,但在MySQL中就比较麻烦了.这次翻译的文章就是 ...
- hdu 4746Mophues[莫比乌斯反演]
Mophues Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 327670/327670 K (Java/Others) Total ...
- VIM 的一些技巧
vim配置文件 ~/.vimrc 如果没有自己创建一个即可 filetype plugin indent on #打开插件 set number #显示行号 syntax on #语法高亮 set c ...
- spark 将dataframe数据写入Hive分区表
从spark1.2 到spark1.3,spark SQL中的SchemaRDD变为了DataFrame,DataFrame相对于SchemaRDD有了较大改变,同时提供了更多好用且方便的API.Da ...
- Spark2 Dataset DataFrame空值null,NaN判断和处理
import org.apache.spark.sql.SparkSession import org.apache.spark.sql.Dataset import org.apache.spark ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- Laravel 查询包括软删除的记录
查询结果包括已被软删除的记录: Model::withTrashed()->get(); 只查询软删除记录: Model::onlyTrashed()->get(); PS:个人博客-La ...
- Alice, Bob, Oranges and Apples CodeForces - 586E
E - Alice, Bob, Oranges and Apples CodeForces - 586E 自己想的时候模拟了一下各个结果 感觉是不是会跟橘子苹果之间的比例有什么关系 搜题解的时候发现了 ...
- ResourceManager High Availability
Introduction This guide provides an overview of High Availability of YARN’s ResourceManager, and det ...
- uboot 下更改NAND的分区 fdisk
uboot 下更改NAND的分区 fdisk 分类: S5PXX(三星)2012-07-01 18:59 8946人阅读 评论(7) 收藏 举报 flash平台cacheandroid三星null 关 ...