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 ...
随机推荐
- RedHat 简易配置 VNC Server 与VNC View详细说明
首先下载Linux版本的VNC文件. 下载地址:http://www.realvnc.com/download/vnc/ 如:VNC-5.0.2-Linux-x86-RPM.tar.gz(其实解压出来 ...
- 自定义vue全局组件use使用
自定义一个全局Loading组件,并使用:总结目录:|-components |-loading |-index.js 导出组件,并且install |-loading.vue 定义Loading组件 ...
- BC32206 错误
出现这种情况的话 排除代码引入dll版本的原因 看下是不是 你之前用net reflector 生成了pdb文件 导致项目引入的版本都是他生成的文件 处理方案就是 用everything 找到这个dl ...
- 代码使用了php的包管理器composer,include到你的php脚本
使用数据库进行crontab配置管理,除非你能够保证数据库的请求能够在长时间内保持稳定响应的话.推荐使用nosql类型的cache存储,同时做好持久化备份. 测试代码: define('DS', DI ...
- 51nod 1432 - 独木舟 - [贪心]
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1432 基准时间限制:1 秒 空间限制:131072 KB ...
- XTU 1264 - Partial Sum - [2017湘潭邀请赛E题(江苏省赛)]
2017江苏省赛的E题,当时在场上看错了题目没做出来,现在补一下…… 题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id ...
- Kettle 4.2源码分析第四讲--KettleJob机制与Database插件简介(含讲解PPT)
1. Job机制 一个job项代表ETL控制流中的一项逻辑任务.Job项将会顺序执行,每个job项会产生一个结果,能作为别的分支上job项的条件. 图 1 job项示例 1.1. Job类图简介 图 ...
- Linux/Unix命令行安装weblogic软件
--通过java -jar wls1036_generic.jar启动安装weblogic软件进程: [weblogic@localhost mnt]$ java -jar wls1036_gener ...
- SVN里直接把本地目录纳入管理
如果本地有个已有的目录,要直接纳入SVN管理,怎么办呢?直接在Repository Browser里面 Add folder,但这样虽然把目录加到SVN,但本地目录没有纳入管理,你还要重新又下到本地才 ...
- php Only variables can be passed by reference
最近做项目,发现了一个报错 Only variables can be passed by reference, 意思是"只有变量能通过'引用'" 就是在代码中 使用了一个方法 ...