【LeetCode】Evaluate Reverse Polish Notation
来源:互联网 发布:arm linux gcc官网 编辑:程序博客网 时间:2024/04/29 06:55
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?
class Solution {public: bool isoperator(string op) { if(op == "+" || op == "-" || op == "*" || op == "/") return true; else return false; } int compute(int op1, int op2, string op) { if(op == "+"){ return op1+op2; } else if(op == "-"){ return op1-op2; } else if(op == "*"){ return op1*op2; } else return op1/op2; } int evalRPN(vector<string> &tokens) { if(tokens.size() == 1){return atoi(tokens[0].c_str()); } stack<string> buf; for(int i = 0; i < tokens.size(); i++) { if(isoperator(tokens[i])){string op1 = buf.top();int ope1 = atoi(op1.c_str());buf.pop();string op2 = buf.top();int ope2 = atoi(op2.c_str());buf.pop();int tmp = compute(ope2,ope1,tokens[i]);if(i == tokens.size()-1){return tmp;}else {char buffer[64];//itoa(tmp,buffer,10);sprintf(buffer,"%d",tmp);string res(buffer);buf.push(res);}}else buf.push(tokens[i]); } }};
0 0
- Evaluate Reverse Polish Notation | leetcode
- leetcode-Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode - Evaluate Reverse Polish Notation
- LeetCode | Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation (LeetCode)
- leetcode: Evaluate Reverse Polish Notation
- Leetcode Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation -- LeetCode
- 【LeetCode】Evaluate Reverse Polish Notation
- LeetCode-Evaluate Reverse Polish Notation
- LeetCode:Evaluate Reverse Polish Notation
- LeetCode Evaluate Reverse Polish Notation
- Leetcode:Evaluate Reverse Polish Notation
- 金银价格,
- 不如亲临剧
- 想做一个整合开源安全代码扫描工具的代码安全分析平台
- c#开发WinCE程序之sqlce安装
- Apache Mina的SSL连接API
- 【LeetCode】Evaluate Reverse Polish Notation
- SQL语言分类
- Ubuntu“无法解析或打开软件包的列表或是状态文件”的解决办法。
- Single Number I & II
- 读《黑客与画家》
- Mat,图像的新容器
- maven项目的坐标和依赖
- Multiple markers at this line - The type java.lang.String cannot be resolved.
- 推荐java书籍