150. Evaluate Reverse Polish Notation
来源:互联网 发布:冒险岛钻机的通讯数据 编辑:程序博客网 时间:2024/05/22 05:30
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
一个栈的案例:
int evalRPN(vector<string>& tokens) { if (tokens.size() == 0)return 0; stack<int> res; int sum = 0; for (int i = 0; i < tokens.size(); i++){ if (tokens[i] != "+" && tokens[i] != "-" && tokens[i] != "*" && tokens[i] != "/")res.push(stoi(tokens[i])); else{ int temp2 = res.top(); res.pop(); int temp1 = res.top(); res.pop(); if (tokens[i] == "+") res.push(temp1 + temp2); else if (tokens[i] == "-") res.push(temp1 - temp2); else if (tokens[i] == "*") res.push(temp1 * temp2); else if (tokens[i] == "/") res.push(temp1 / temp2); } } return res.top();}
阅读全文
0 0
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 04_BootScrap——CRUD(动态页面上篇)
- 《珠珠图案》创意——发挥聪明才智,灵活应用软件,创造性扩展。
- 人脸识别趟坑历程
- Android listview 右滑删除
- A simple java application.
- 150. Evaluate Reverse Polish Notation
- 人脸识别(face recognition)
- 2015湖南省赛 CSU 1777 : 大还是小? (水)
- Using RequireJS in AngularJS Applications
- 通过简单示例来理解什么是机器学习
- xUtils
- N!
- C#Lesson01
- 在线JS转C#代码