Evaluate Reverse Polish Notation --leetcode
原题链接:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
题目大意:给出逆波兰式,然后求其结果。
解题方法:单个栈
思路:遍历逆波兰式,若为数字。则入栈。若为操作符。则弹出栈顶的2个元素,然后将其相应该操作符的结果入栈。遍历完毕后,栈中元素就是所求结果。
时间复杂度:O(N) 空间复杂度 : O(1)
class Solution {
public:
int evalRPN(vector<string> &tokens) {
if(tokens.empty())
return 0;
stack<int> s;
int op1=0,op2=0;
for(int i=0;i<tokens.size();i++)
{
if(tokens[i]=="+")
{
op2=s.top();s.pop();op1=s.top();s.pop();s.push(op1+op2);
}
else if(tokens[i]=="-")
{
op2=s.top();s.pop();op1=s.top();s.pop();s.push(op1-op2);
}
else if(tokens[i]=="*")
{
op2=s.top();s.pop();op1=s.top();s.pop();s.push(op1*op2);
}
else if(tokens[i]=="/")
{
op2=s.top();s.pop();op1=s.top();s.pop();s.push(op1/op2);
}
else
s.push(atoi(tokens[i].c_str()));
}
return s.top();
}
};
Evaluate Reverse Polish Notation --leetcode的更多相关文章
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- Evaluate Reverse Polish Notation——LeetCode
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Evaluate Reverse Polish Notation leetcode java
题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
随机推荐
- 环境变量HISTCONTROL命令及对快捷键Ctrl+o命令的影响
在linux中环境变量HISTCONTROL可以控制历史的记录方式. HISTCONTROL有以下的选项: ignoredups 默认,忽略重复命令 ignorespace ...
- Python 2 和 Python 3 主要区别有哪些(1)
Guido(Python之父,仁慈的独裁者)在设计 Python3 的过程中,受一篇文章 “Python warts” 的影响,决定不向后兼容,否则无法修复大多数缺陷.---摘录自<流畅的Pyt ...
- Java学习笔记(1)-(GridBagLayout)网格袋布局
学习JAVA-布局管理的时候,在书上看到了这么一段话:GridBagLayout的功能非常强大,使用是也比较复杂,考虑到一般的读者很少会使用到这种管理,这里不做介绍.然书本就跳过了,为什么功能强大却很 ...
- He'llWorld_Struts2
[步骤] 1.创建web项目 2.导入相关jar包 3.配置核心过滤器 web app libraris ---> struts-core ---> org.apache.struts2 ...
- php 面向对象 (类 对象)
//面向对象//什么是面向对象//面向过程//什么是对象?//一切皆是对象//类//由对象抽象化//造类//class Ren//{ //构造方法 - - 写不写都存在//类的初始化方法 //构造方法 ...
- 【树形DP】codeforces K. Send the Fool Further! (medium)
http://codeforces.com/contest/802/problem/K [题意] 给定一棵树,Heidi从根结点0出发沿着边走,每个结点最多经过k次,求这棵树的最大花费是多少(同一条边 ...
- hdu 4460spfa用map来实现
#include<stdio.h> #include<string.h> #include <iostream> #include <algorithm& ...
- [Usaco2009 Open]工作安排Job
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1457 Solved: 687[Submit][Status][Discuss] Descriptio ...
- HDU 6370 dfs+并查集
Werewolf Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- 2017-10-04-morning
改题面只有1改为0 .. #include <cstring> #include <cstdio> inline void read(int &x) { x=; reg ...