Evaluate Reverse Polish Notation

Total Accepted: 24595 Total Submissions: 123794My Submissions

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

Valid operators are +-*/. Each operand may be an integer or another expression.

Some examples:

  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
Have you been asked this question in an interview? 

思路:栈的简单应用

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <cstdlib> using namespace std; class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> s;
string str;
int a, b; for (int i = ; i < tokens.size(); i++) {
str = tokens[i];
if (str == "+" || str == "-" || str == "*" ||
a = s.top(); s.pop();
b = s.top(); s.pop();
switch(str[]) {
case '+': s.push(b+a); break;
case '-': s.push(b-a); break;
case '*': s.push(b*a); break;
case '/': s.push(b/a); break;
}
}
else {
s.push(atoi(str.c_str()));
}
}
a = s.top();s.pop();
return a;
}
}; int main(int argc, char *argv[]) {
Solution* solution = new Solution();
vector<string> tokens;
tokens.push_back("");
tokens.push_back("");
tokens.push_back("+");
tokens.push_back("");
tokens.push_back("*"); cout << solution->evalRPN(tokens) << endl; vector<string> tokens2;
tokens2.push_back("");
tokens2.push_back("");
tokens2.push_back("");
tokens2.push_back("/");
tokens2.push_back("+");
cout << solution->evalRPN(tokens2) << endl;
return ;
}

leetcode - [2]Evaluate Reverse Polish Notation的更多相关文章

  1. 【leetcode】Evaluate Reverse Polish Notation

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

  2. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

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

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

  4. 【leetcode】Evaluate Reverse Polish Notation(middle)

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

  5. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

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

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

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

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

  8. LeetCode——150. Evaluate Reverse Polish Notation

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

  9. Leetcode#150 Evaluate Reverse Polish Notation

    原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...

随机推荐

  1. VS2010下MFC的串口编程

    串口通信简介 一般来说,计算机都有一个或多个串行端口,这些串口提供了外部设备与PC进行数据传输和通信的通道,在CPU和外设之间充当解释器的角色.当字符数据从CPU发送给外设时,这些字符数据将被转换成串 ...

  2. C++连接MySQL(Windows)

    一般来说,VS下采用微软自身的SQL Server是比较常见的做法,但SQL Server只适合学习,不适合真正应用.在此,我们选择MySQL作为后台数据库.C++语言本身并没有提供访问数据库的东西, ...

  3. 繁体简体转化_langconv.py

    from copy import deepcopyimport re try: import psyco psyco.full()except: pass try: from zh_wiki impo ...

  4. WCF 与 Windows Store Client App

    首先复习下WCF: WCF实际上是构建了一个框架,这个框架实现了在互联系统中各个Application之间如何通信.使得Developers和Architect在构建分布式系统中,无需在考虑如何去实现 ...

  5. 2017-2018-1 20155312《信息安全技术》实验二——Windows口令破解实验报告

    2017-2018-1 20155312<信息安全技术>实验二--Windows口令破解实验报告 实验目的 了解Windows口令破解原理 对信息安全有直观感性认识 能够运用工具实现口令破 ...

  6. 20155312 2016-2017-2 《Java程序设计》第九周学习总结

    20155312 2016-2017-2 <Java程序设计>第九周学习总结 课堂内容总结 两个类有公用的东西放在父类里. 面向对象的三要素 封装 继承 多态:用父类声明引用,子类生成对象 ...

  7. 9月list

    开学了,我已经是大三的老学姐了,难受! 哇,时间过得好快啊,感觉自己快毕业了,肿么办!!! 9月了,快一年了,其实很多东西都变了,比如你. 9月4日的list:

  8. ubuntu下安装/卸载vmware虚拟机

    1.下载vmware(官网下载试用版,试用版输入序列号后即为专业版,序列号网上搜,很多) 2.下载后安装(命令行) 1)cd进你下载的位置 1.1)下载的文件名字为:VMware-Workstatio ...

  9. AOP 环绕通知 (Schema-base方式) 和 AspectJ方式在通知中获取切点的参数

    环绕通知(Schema- base方式) 1.把前置通知和后置通知都写到一个通知中,组成了环绕通知 2.实现步骤: 2.1 新建一个类实现 MethodInterceptor 接口 public cl ...

  10. mysql之零碎知识

    一 视图 什么是视图:视图就是一张虚拟表.方便查看. 创建视图:create view 起名 as sql语句 #两张有关系的表 mysql> select * from course; +-- ...