Leetcode Evaluate Reverse Polish Notation
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 此题是求解后缀表达式,题目比较简单,开一个数据stack即可,此题的改进版本是引入括号,此时要开一个数据stack和运算符stack
bool isOperator(string& op){
if(op == "+" || op == "-" || op == "*" || op == "/") return true;
else return false;
} int calc(int a, int b, char op){
switch(op){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
} int evalRPN(vector<string> &tokens){
stack<int> num;
for(int i = ; i < tokens.size(); ++ i){
if(!isOperator(tokens[i])){
num.push(atoi(tokens[i].c_str()));
}else{
int num2 = num.top(); num.pop();
int num1 = num.top(); num.pop();
char op = tokens[i][];
num.push(calc(num1,num2,op));
}
}
return num.top();
}
Leetcode Evaluate Reverse Polish Notation的更多相关文章
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- [leetcode]Evaluate Reverse Polish Notation @ Python
原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...
- leetcode——Evaluate Reverse Polish Notation 求算式值(AC)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] Evaluate Reverse Polish Notation stack 栈
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] Evaluate Reverse Polish Notation [2]
题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, ...
- 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 ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
随机推荐
- 360极速浏览器安装.crx扩展(postman)
用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具.今天给大家介绍的这款网页调试工具不仅可以 ...
- Linux SNMP oid
http://www.debianadmin.com/linux-snmp-oids-for-cpumemory-and-disk-statistics.html
- c++11的初始化
c++11 中类型初始更加方便 比如 vector<int> vec = {1,2,3}; vector<int> vec{1,2,3}; map<string, ...
- 对Object类中方法的深入理解
看一下API中关于Object的介绍: 类 Object 是类层次结构的根类.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法. 那么Object中到底有哪些方法,各自有什 ...
- sdut 2445 小学数学
小学数学 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...
- Oracle优化-表设计
前言 绝大多数的Oracle数据库性能问题都是由于数据库设计不合理造成的,只有少部分问题根植于Database Buffer.Share Pool.Redo Log Buffer等内存模块配置不合理, ...
- hdu 5033 单调栈 ****
看出来是单调栈维护斜率,但是不会写,2333,原来是和询问放在一起的 #include <iostream> #include <cstdio> #include <cs ...
- C++重载覆盖隐藏
写一个程序,各写出重载覆盖 1 // // main.cpp // 2013-7-17作业2 // // Created by 丁小未 on 13-7-17. // Copyright (c) 201 ...
- Mac系统修改Intellij Idea默认JDK版本
Intellij IDEA 默认情况下,使用的jdk的版本是1.6,当第一次启动IDEA的时候,如果系统中未安装jdk,则系统会自动到苹果官网下载jdk安装文件.如果你的系统已经安装了jdk1.7或是 ...
- X64下MmIsAddressValid的逆向及内存寻址解析
标 题: [原创]X64下MmIsAddressValid的逆向及内存寻址解析 作 者: 普通朋友 时 间: 2015-10-21,20:03:52 链 接: http://bbs.pediy.com ...