原题链接: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的更多相关文章

  1. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  2. Evaluate Reverse Polish Notation——LeetCode

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  3. Evaluate Reverse Polish Notation leetcode java

    题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...

  4. 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 ...

  5. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

  6. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  7. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  8. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  9. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

随机推荐

  1. 如何用纯 CSS 创作一种按钮被瞄准的交互特效

    效果预览 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. 在线演示 https://codepen.io/zhang-ou/pen/ELWMLr 可交互视频教程 此视 ...

  2. 如何学好C和C++

    酷壳上的两篇文章,转载至此,学好C和C++. 我相信,这可能是很多朋友的问题,我以前也有这样的感觉,编程编到一定的时候,发现能力到了瓶颈,既不深,也不扎实,半吊子.比如:你长期地使用Java和.NET ...

  3. Android设置透明状态

    xml中: android:background="@android:color/transparent" 半透明: android:background="#e0000 ...

  4. 算法导论 第九章 中位数和顺序统计量(python)

    第i个顺序统计量:该集合中第i小的元素(建集合排序后第i位 当然算法可以不排序) 中位数:集合中的中点元素 下中位数 上中位数 9.1最大值和最小值 单独的max或min每个都要扫一遍 n-1次比较 ...

  5. 【转】C# windows服务的创建与调试

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  6. xtu summer individual-4 B - Party All the Time

    Party All the Time Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Orig ...

  7. hexo干货系列:(四)将hexo博客同时托管到github和coding

    前言 之前我们把hexo托管在github,但是毕竟github是国外的,访问速度上还是有点慢,所以想也部署一套在国内的托管平台,之前查资料听说gitcafe,但是听说gitcafe已经被coding ...

  8. hdu1856 选出更多的孩子

    题目大意: 老师选取2个学生对应的号码,这两人视作朋友,同时朋友的朋友也可以看成自己的朋友. 最后老师选出一个人数最多的朋友圈. 这里学生的人数不大于10^7,所以操作时需要极为注意,操作步数能省则省 ...

  9. 【链表】2017多校训练三 HDU 6058 Kanade's sum

    acm.hdu.edu.cn/showproblem.php?pid=6058 [题意] 给定一个排列,计算 [思路] 计算排列A中每个数的贡献,即对于每个ai,计算有ni个区间满足ai是区间中的第k ...

  10. springboot注释详解

    1.属性注入 @ConfigurationProperties(prefix="...") spring会从classpath下的/config目录或者classpath的根目录查 ...