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 ...
随机推荐
- (9) openssl enc(对称加密)
对称加密工具,了解对称加密的原理后就很简单了,原理部分见下文. openssl enc -ciphername [-in filename] [-out filename] [-pa ...
- python读取excel学习(1)
#coding=gbk #coding=utf-8 import xlrd table = xlrd.open_workbook(r'E:\test.xlsx') #sheet = table.she ...
- php函数之数组
关联数组 isset bool isset( mixed $val [, mix $...]) 变量是否已设置并且非null.多个参数从左到右计算. 判断null $a=null;var_dump(i ...
- 根据不同的产品id获得不同的下拉选项 (option传多值)
<td> 没有 value 所以要在<td>里面加上input 同时text 为hidden这样就不会显示value的值 <td><select id='g ...
- 03002_Http请求协议分析
1.编写一个form.html的表单页面 (1)使用EclipseEE新建一个动态的web项目: (2)Dynamic web module version选择2,5版本: (3)新建一个form.h ...
- mysql 递归查询父节点 和子节点
查父集合 --drop FUNCTION `getParentList` )) ) BEGIN ) default ''; ) default rootId; WHILE rootId is not ...
- SQL2012通用分页存储过程
--提取分页数据,返回总记录数 Createprocedure [dbo].[sp_Common_GetDataPaging_ReturnDataCount] ( @SqlString varchar ...
- [luoguP3068] [USACO13JAN]派对邀请函Party Invitations(stl大乱交)
传送门 记录每一个编号在那些组中,可以用vector,这里选择链式前向星. 每一组用set 将被邀请的放到queue中 #include <set> #include <queue& ...
- BZOJ1986: [USACO2004 Dec] Dividing the Path 划区灌溉
L<=1000000的土地上用长度在2*A~2*B的线段覆盖所有点,且给定n<=1000个区间,每个区间上只允许有一条线段,求最少多少线段,无解-1. f[i]表示填前i个土地最少线段,f ...
- BZOJ2099: [Usaco2010 Dec]Letter 恐吓信
给两个长度不超过50000的串,A串可每次截连续一段复制出来,求最少复制几次能得到B串. 方法一:SAM.不会. 嗯好会了. #include<stdio.h> #include<s ...