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 ...
随机推荐
- 配置github SSH公钥登录
git的安装见https://www.cnblogs.com/liliyang/p/9829931.html 配置git使用ssh密钥 git支持https和git两种传输协议,github分享链接时 ...
- 前端基础之CSS_2
摘要 盒子模型 浮动 清除 溢出 定位 模态框 rgba 与 opacity透明区别 一.CCS盒子模型 margin:标签与标签之间的距离,主要用于控制标签周围间的间隔,从视觉上达到相互分开的目的 ...
- css选择器(2)——属性选择器和基于元素结构关系的选择器
在有些标记语言中,不能使用类名和id选择器,于是css2引入了属性选择器. 3.属性选择器 a)根据是否存在该属性来选择 如果希望选择有某个属性的元素,例如要选择有class属性的所有h1元素,使其文 ...
- 我的java web之路(JSP基本语法)
1.JSP注释 1.1输出注释 语法格式 <!--comment [<%= expression %>] --> <body> This is my JSP pa ...
- cxLookupCombobox的多字段模糊匹配
查了网上很多资料,懒人输入:通过程序使用过滤对话达到自己的目的: 用到cxFilter单元: cbb_DoctorOrder.Properties.View.DataController.Filter ...
- Oracle数据库学习之存储过程--提高程序执行的效率
存储过程是Oracle开发者在数据转换或查询报表时经常使用的方式之一.它就是想编程语言一样一旦运行成功,就可以被用户随时调用,这种方式极大的节省了用户的时间,也提高了程序的执行效率.存储过程在数据库开 ...
- Leetcode 239.滑动窗口最大值
滑动窗口最大值 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位. 返回滑动窗口最大值. 示例: ...
- Go map例题
package main import "fmt" //map例题 //寻找最长不含有重复字符的子串 // abcabcbb -> abc //pwwkew ->wke ...
- 【数学】codeforces C. Maximal GCD
http://codeforces.com/contest/803/problem/C [题意] 给定两个数n,k(1 ≤ n, k ≤ 10^10) 要你输出k个数,满足以下条件: ①这k个数之和等 ...
- 魔咒词典(hdu 1880)
Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...