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. 移动端IOS和androi及浏览器js判断[转载]

    转载自:http://www.niutifa.com/?p=561 移动端IOS和androi及浏览器js判断: <script type="text/javascript" ...

  2. HTML&&css练习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. javascript的变量类型:var、let、const

    不同点:可变性,与作用域的关系. 可变性:const定义的变量都不可变,而var和let可以任意更改. const 只能在声明时被初始化一次,之后不允许将全新的值赋值给const变量.但可以修改con ...

  4. UEFI、BIOS、Secure Boot的关系和知识介绍

      从Windows 8操作系统时代开始,安装操作系统的方法也有了很大的改变,Windows 8采用了Secure Boot引导启动的方式,而不是过去Win XP和Win 7的Legacy启动方式,从 ...

  5. 使用Trinity拼接以及分析差异表达一个小例子

    使用Trinity拼接以及分析差异表达一个小例子  2017-06-12 09:42:47     293     0     0 Trinity 将测序数据分为许多独立的de Brujin grap ...

  6. 判断and ,or

    and 和 or 是条件 与和或,记住一条,and 是两边同时都满足,or  是只有满足一个条件就成立. # print(1 or False) #条件1成立,条件2不成立.打印条件1 #返回: 1# ...

  7. HTTP 1.0 Status Code Definitions

    part of Hypertext Transfer Protocol -- HTTP/1.1RFC 2616 Fielding, et al. 10 Status Code Definitions ...

  8. datagrid 扩展 页脚 合计功能

    效果图:合计信息展示在页脚中(showFooter:true) code: <!DOCTYPE html> <html> <head> <meta chars ...

  9. JSON数据映射之元素可见控制

    1.效果: 2.demo 源码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  10. window下文件在Linux下文件乱码解决

    在使用iconv转换文件的字符编码时,如果遇到类似“iconv: illegal input sequence at position”的错误,原因是需要转换的字符编码没有涵盖文件中的字符,比如,将一 ...