150. Evaluate Reverse Polish Notation
来源:互联网 发布:云计算解决方案 编辑:程序博客网 时间:2024/06/18 09:34
/*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 根据逆波兰表达式求值 操作数入栈;遇到操作符时,操作数出栈,求值,将结果入栈;当一遍后,栈顶就是表达式的值*/class Solution {public: int evalRPN(vector<string>& tokens) { if(tokens.empty()) return -1; stack<int> st; int m=0,n=0; for(int i=0;i<tokens.size();i++) { if(tokens[i]=="+" && st.size()>1) { m=st.top(); st.pop(); n=st.top(); st.pop(); st.push(m+n);continue; } if(tokens[i]=="-" && st.size()>1) { m=st.top(); st.pop(); n=st.top(); st.pop(); st.push(n-m);continue; } if(tokens[i]=="*" && st.size()>1) { m=st.top(); st.pop(); n=st.top(); st.pop(); st.push(m*n);continue; } if(tokens[i]=="/" && st.size()>1) { m=st.top(); st.pop(); n=st.top(); st.pop(); st.push(n/m);continue; } st.push(atoi(tokens[i].c_str())); } return st.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
- HDU
- 【CVTE】请使用js编写一个函数:chunk(arr,size),把一个数组arr按照指定的数组大小size分割成若干个数组块,传入的参数分别为数组
- Tex Studio配置字典用于检查拼写
- 旨在物联网领域拔得头筹,LoRa成为运营商重要武器
- Apache Kafka
- 150. Evaluate Reverse Polish Notation
- vs2010 无法将 复制到 超出了重试计数10
- CentOS6.8 详细安装步骤
- Redis、Memcache和MongoDB的区别
- Http请求中Content-Type
- SPSS Modeler中对字段指定的类型不充分的解决方法
- LeetCode-12-Integer-to-Roman 无聊模拟题
- python学习笔记-马哥2017
- 关于Java设计模式