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. centos 命令学习

    关机&重启 shutdown -h 10          #计算机将于10分钟后关闭,且会显示在登录用户的当前屏幕中 shutdown -h now       #计算机会立刻关机 shut ...

  2. js验证前后密码是否一致,为什么当我输入不一致密码时,不会弹出警告啊

    <form name="form" action="#"><input type="password" id=" ...

  3. andorid 菜单 进度条

    activity_ui2.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  4. hdu 1394(线段树) 最小逆序数

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 给出一列数组,数组里的数都是从0到n-1的,在依次把第一个数放到最后一位的过程中求最小的逆序数 线段树的应 ...

  5. 反射中,Class.forName 和 classloader 的区别

    https://blog.csdn.net/qq_27093465/article/details/52262340 java中class.forName()和classLoader都可用来对类进行加 ...

  6. Java.Class

    Class类 1. Class继承自Object. 2. .class 和 instance.getClass()的区别 Ref[1] Reference 1. .class http://stack ...

  7. hihoCoder1159 扑克牌

    一道记忆化搜索 原题链接 和着色方案很像,这里就不详细阐述,可以去我博客里的着色方案里看. 但要注意本题不一样的是同种面值的牌花色不同,所以在转移时还需要乘上同种面值的牌的个数. #include&l ...

  8. 通过 ulimit 改善系统性能

    系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何在有限资源的条件下保证程序的运作,ulimit 是我们在处理这些问题时,经常使用的一种简单手段.ulimit 是一种 l ...

  9. 买茶叶想到的哪个比较便宜 x1/y1 >x2/y2 x代表多少钱 y代表 多少克 无聊的试炼

    茶叶1 128元     200克 茶叶2  330元    160克 当然这个哪个便宜 一眼就知道了,这里不过抛砖引玉 128元    330元 200克    160克 我们把价钱用x表示 多少克 ...

  10. go语言练习

    // main package main import ( "fmt" "runtime" "sync" ) func main() { f ...