Evaluate Reverse Polish Notation - LeetCode 150
来源:互联网 发布:乾坤线软件下载 编辑:程序博客网 时间:2024/05/16 11:49
题目描述:
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
Hide Tags Stack
分析:
逆波兰式的计算规则如下:
如果是操作数,则直接压入栈中,如果是运算符,则从栈中弹出两个操作数进行运算后,将结果入栈。
本题的逆波兰是的字符串数组,因此需要注意将字符串转换为整数,同时要注意负数的转换。
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
Hide Tags Stack
分析:
逆波兰式的计算规则如下:
如果是操作数,则直接压入栈中,如果是运算符,则从栈中弹出两个操作数进行运算后,将结果入栈。
本题的逆波兰是的字符串数组,因此需要注意将字符串转换为整数,同时要注意负数的转换。
以下是C++实现代码
/*////////////////16ms//*/class Solution {public: bool isOp(string s) //判断是否为运算符 { if(s == "+" || s == "-" || s == "*" || s == "/") return true; return false; } int stoi(string s) //将字符串转换为整数,注意负数 {int flag = 1;int i = 0,num = 0;if(s[0] == '-'){ flag = -1; //标记负数 i++;} while(i < s.size()){ if(isdigit(s[i]))num = num * 10 + (s[i++] - '0');} return flag * num; } int doOp(int a,int b,string op) //进行a op b运算 { if(op == "+") return a + b; else if(op == "-") return a - b; else if(op == "*") return a * b; else if(op == "/") return a / b; else ; } int evalRPN(vector<string>& tokens) { int len = tokens.size(); //if(len == 1) // return stoi(tokens[0]); stack<int> s; int i = 0;int num = 0; for(; i < len; i++) { if(!isOp(tokens[i])) { num = stoi(tokens[i]); //将操作数入栈 s.push(num); } else //如果是运算符,就从栈中弹出两个操作数,运算后将结果入栈 { int a = 0,b = 0; b = s.top(); s.pop(); a = s.top(); s.pop(); s.push(doOp(a,b,tokens[i])); } } return s.top(); //最后栈里只有一个元素,就是最终结果 }};
0 0
- LeetCode 150 Evaluate Reverse Polish Notation
- LeetCode: Evaluate Reverse Polish Notation [150]
- LeetCode(150) Evaluate Reverse Polish Notation
- [leetcode 150] Evaluate Reverse Polish Notation
- [LeetCode 150]Evaluate Reverse Polish Notation
- Leetcode 150:Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation - LeetCode 150
- LeetCode---(150)Evaluate Reverse Polish Notation
- [Leetcode 150, Medium] Evaluate Reverse Polish Notation
- leetcode[150]:Evaluate Reverse Polish Notation
- LeetCode(150) Evaluate Reverse Polish Notation
- [leetcode-150]Evaluate Reverse Polish Notation(c++)
- leetcode 150: Evaluate Reverse Polish Notation
- 【leetcode】No.150 Evaluate Reverse Polish Notation
- LeetCode(150) Evaluate Reverse Polish Notation
- LeetCode 150 Evaluate Reverse Polish Notation
- leetcode(150):Evaluate Reverse Polish Notation
- Leetcode 150 Evaluate Reverse Polish Notation
- 第三章第四十二题
- 3d数学基础-4x4齐次矩阵-用C++代码实现
- Android事件处理模型
- 使用 JMeter 完成常用的压力测试
- 随想随摘
- Evaluate Reverse Polish Notation - LeetCode 150
- 制作jpg图片中隐藏rar文件
- getchar(),gets,scanf()的区别
- To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
- 汇编实验-10-1 显示字符串
- 第三章 第三十八题
- Linux修改权限chmod
- JAVA敏捷开发环境搭建
- Primavera P6R15.1 EPPM 下载(截稿)