原题地址

基本栈操作。

注意数字有可能是负的。

代码:

  1. int toInteger(string &s) {
  2. int res = ;
  3. bool negative = s[] == '-' ? true : false;
  4.  
  5. for (int i = negative ? : ; i < s.length(); i++) {
  6. res *= ;
  7. res += s[i] - '';
  8. }
  9. return negative ? -res : res;
  10. }
  11.  
  12. int compute(int a, int b, string sign) {
  13. switch (sign[]) {
  14. case '+':
  15. return a + b;
  16. case '-':
  17. return a - b;
  18. case '*':
  19. return a * b;
  20. case '/':
  21. return a / b;
  22. default:
  23. return -;
  24. }
  25. }
  26.  
  27. int evalRPN(vector<string> &tokens) {
  28. stack<int> st;
  29.  
  30. for (auto t : tokens) {
  31. if (t.length() > || t[] >= '' && t[] <= '')
  32. st.push(toInteger(t));
  33. else {
  34. int b = st.top();
  35. st.pop();
  36. int a = st.top();
  37. st.pop();
  38. st.push(compute(a, b, t));
  39. }
  40. }
  41.  
  42. return st.top();
  43. }

Leetcode#150 Evaluate Reverse Polish Notation的更多相关文章

  1. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

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

  2. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

  3. leetcode 150. Evaluate Reverse Polish Notation ------ java

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

  4. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

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

  5. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

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

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

  7. 150. Evaluate Reverse Polish Notation - LeetCode

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

  8. 【LeetCode】150. Evaluate Reverse Polish Notation

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

  9. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

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

随机推荐

  1. Web前端性能优化的9大问题

    1.请减少HTTP请求基本原理:在浏览器(客户端)和服务器发生通信时,就已经消耗了大量的时间,尤其是在网络情况比较糟糕的时候,这个问题尤其的突出.一个正常HTTP请求的流程简述:如在浏览器中输入&qu ...

  2. 为hbase新增节点

    为hbase增加新的节点,首先要为hadoop增加新新街点.因为我的做法是将datanode和regionserver放到一台物理机上.因此大体流程是: 1.克隆已经存在的regionserver虚拟 ...

  3. CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。

    firewall:systemctl start firewalld.service#启动firewallsystemctl stop firewalld.service#停止firewallsyst ...

  4. 包装类(Wrapper Class)

    1)包装类.针对于原生数据类型的包装.所有的包装类(8个)对位于java.lang包下.java中的8个包装类分别是:Byte,Short,Integer,Long,Float.Double,Char ...

  5. String类详解,StringBuffer

    先说一下String类的equals()方法. 下面我们先看一段代码: 这段代码输出的结果为: ture true -------------- false 咋看之下貌似Object类比较特别,那么我 ...

  6. ORA-08189

    OS: [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 DB: Oracle Database 11g E ...

  7. 关于生成缩略图及水印图片时出现GDI+中发生一般性错误解决方法

    System.Drawing.Image OldImage = null; oldImage = System.Drawing.Image.FromFile(ImageUrl); 使用该方法读取图片时 ...

  8. sed实例一则

    1.背景: test.txt文件里有这些语句 li^E1026^D20150802B07QH800^B698.^C20150801B08CDP00^B514.^C20150803D00A8L00^B2 ...

  9. JavaScript高级程序设计之函数

    函数实际上是对象,每个函数都是Function类型的实例. 函数是引用类型. 函数名实际上是一个指向函数对象的指针,不会与某个函数绑定. // 这种写法更能表达函数的本质 var sum = func ...

  10. Windows Phone中In-App Purchase应用内购买

    前言       应用内购买(In-App Purchase)对于开发者来说绝对是一个非常重要的功能,它提供了一个便捷的入口供用户来购买付费.在IAP盛行之前的游戏运营商一般都是通过接入第三方支付入口 ...