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 ...
随机推荐
- LINUX:关于Redis集群搭建 、和搭建项目中遇到的问题
文章来源:http://www.cnblogs.com/hello-tl/p/7804225.html 0.Redis的简单安装 1.安装redis依赖 # yum install gcc tcl g ...
- 剑指Offer(书):打印从1到最大的n位数
题目:输入数字N,按顺序打印出从1到最大的N位十进制数,比如输入3,则打印出1.2.3一直到999 分析:N的范围不定.所以有可能超出范围,因此用数组存放以及输出.说实话,对复杂递归还是一头雾水 pu ...
- 完善的IaaS云服务的个人理解
此文已由作者王盼授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前情提要 本文仅讨论云主机(虚拟机).云硬盘(块存储).云网络(普通虚拟网络或SDN)相关的IaaS服务,相关 ...
- 一个可以获取知乎timeline的爬虫
# -*- coding: utf-8 -*- import requests import lxml import os,time from bs4 import BeautifulSoup as ...
- 什么是Istio
本文主要是对Istio Prelim 1.0(https://preliminary.istio.io/docs/)的翻译 Istio:一种开放式平台,用于连接,管理和保护微服务. Istio提供了一 ...
- Hadoop JVM调整解决 MapReduce 作业超时问题
摘要:由于业务需要,在mapreduce汇总时需要关联两个基础表,一个60M左右,不影响mr运行,另一个表,大小约为380M,行数为1700万行左右,在默认配置下,一旦加载这个数据就会在reduce阶 ...
- bzoj5090组题 分数规划
组题 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 542 Solved: 114[Submit][Status][Discuss] Descript ...
- react.js 高阶组件----很简单的实例理解高阶组件思想
调试代码之前,我设置了两个缓存 分别是username和content 在控制台console设置两个缓存代码 localStorage.setItem('username','老王')localSt ...
- Codevs 1497 取余运算== 洛谷P 1226
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入b,p,k的值,编程计算bp mod k的值.其中的b,p,k*k ...
- 蓝桥杯 算法训练 最短路 [ 最短路 bellman ]
传送门 算法训练 最短路 时间限制:1.0s 内存限制:256.0MB 锦囊1 锦囊2 锦囊3 问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证 ...