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 ...
随机推荐
- java中的编译时与运行时
----?基础知识 -- 编译时 编译器将源代码翻译成机器能够读懂的代码,如java中就是翻译成jvm能够读懂的字节码文件.简单说,编译时就是机器帮我们检查代码是否有出现语法错误,关键字写错之类的 ...
- 用Jenkins构建项目实战
登录Jenkins,新建任务 输入一个任务名称,选择一个项目类型 使用自定义工作空间:使该项目独立于系统的工作空间 自动从Git下载源码,点击添加可以增加凭证 日程表的参数: 第一个参数代表的是分钟 ...
- MySQL sys Schema
MySQL sys Schema 使用sys Schema的先决条件 使用sys Schema sys Schema Progress Reporting sys Schema Object Refe ...
- 宝塔apache配置
apache配置 <VirtualHost *:80> ServerAdmin webmaster@example.com DocumentRoot "/www/wwwroot/ ...
- 前端基础之CSS_2
摘要 盒子模型 浮动 清除 溢出 定位 模态框 rgba 与 opacity透明区别 一.CCS盒子模型 margin:标签与标签之间的距离,主要用于控制标签周围间的间隔,从视觉上达到相互分开的目的 ...
- Android开发——查询/杀死手机里正在运行的进程
0. 前言 以前有同学好像做过一个叫"自习君"的App,开启后自动检测用户这一天的自习时间,在学校里宣传广告还打了不少.其实实现原理非常简单,在SQlite数据库(也可以通过文件) ...
- 【Codeforces 140A】New Year Table
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 算出来每个盘子要占用多少角度. 然后乘n看看是不是小于等于2π就好 (精度最好定在1e-7) [代码] #include <bits/s ...
- Leetcode 312.戳气球
戳气球 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * n ...
- 67. @Transactional的类注入失败【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] Spring的代理模式有两种:java自带的动态代理模式和cglib代理模式,cglib代码模式适用于没有接口的类,而java自带适用于接口类,默 ...
- C#通信学习(一)
基础知识 TCP/IP:Transmission Control Protocol/Internet Protocol,传输控制协议/因特网互联协议,又名网络通讯协议.简单来说:TCP控制传输数据,负 ...