4.1.4—栈—Evaluate Reverse Polish Notation
来源:互联网 发布:现在开童装淘宝店 编辑:程序博客网 时间:2024/06/08 07:04
描述
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
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
#include<iostream>#include<string>#include<stack>#include<vector>#include <sstream>using namespace std;bool Isoperate(string str){if (str == "+" || str == "-" || str == "*" || str == "/")return true;elsereturn false;}int EvaluateReversePolish(vector<string> str){stack<string> mystack;for (int i = 0; i<str.size(); i++){if (!Isoperate(str[i]))mystack.push(str[i]);else{int x = atoi(mystack.top().c_str());mystack.pop();int y = atoi(mystack.top().c_str());mystack.pop();if (str[i] == "+")x = x + y;else if (str[i] == "-")x = x - y;else if (str[i] == "*")x = x*y;else if (str[i] == "/")x = y / x;//===string temp;ostringstream oss;oss << x;temp = oss.str();mystack.push(temp);}}return atoi(mystack.top().c_str());}int main(){vector<string > str;str.push_back("4");str.push_back("13");str.push_back("5");str.push_back("/");str.push_back("+");int res = EvaluateReversePolish(str);cout << res << endl;}
阅读全文
0 0
- 4.1.4—栈—Evaluate Reverse Polish Notation
- 4.1.4 Evaluate Reverse Polish Notation
- LeetCode——Evaluate Reverse Polish Notation
- LeetCode——Evaluate Reverse Polish Notation
- Algorithms—150.Evaluate Reverse Polish Notation
- LeetCode150—Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- 4.1.3—栈—Largest Rectangle in Histogram
- TypeError: cannot perform reduce with flexible type
- 5.1 Tensorflow:图与模型的加载与存储
- docker
- select和epoll的区别
- 4.1.4—栈—Evaluate Reverse Polish Notation
- impala保存时间类型timestamp---NanoTimeUtils根据JdateTime生成nanotime
- xshell5 的账号密码搬家
- 5.1.1—二叉树的遍历—Binary Tree Preorder Traversal
- STM32F407单片机SDIO接口卡在SDIO_STA_CMDACT上无法发送命令的解决方法
- 邻接表 的 DFS 与 BFS
- JavaScript调用UEditor相册模块
- sqlServer自定义函数
- 【数论-莫比乌斯】hdu 2841 Visible Trees