150. Evaluate Reverse Polish Notation (Stack)
2015-10-04 10:11
507 查看
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are
Some examples:
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) { stack< int > operandStack; int operand1; int operand2; for(int i = 0; i < tokens.size(); i++){ if(tokens[i]=="+"){ operand1 = operandStack.top(); operandStack.pop(); operand2 = operandStack.top(); operandStack.pop(); operand2 += operand1; operandStack.push(operand2); } else if(tokens[i]=="-"){ operand1 = operandStack.top(); operandStack.pop(); operand2 = operandStack.top(); operandStack.pop(); operand2 -= operand1; operandStack.push(operand2); } else if(tokens[i]=="*"){ operand1 = operandStack.top(); operandStack.pop(); operand2 = operandStack.top(); operandStack.pop(); operand2 *= operand1; operandStack.push(operand2); } else if(tokens[i]=="/"){ operand1 = operandStack.top(); operandStack.pop(); operand2 = operandStack.top(); operandStack.pop(); operand2 /= operand1; operandStack.push(operand2); } else{ operand1 = atoi(tokens[i].c_str()); operandStack.push(operand1); } } return operandStack.top(); } };
相关文章推荐
- lua脚本的基本语法(二)
- ubuntu15.04编译lua源码
- lua.c:80:31: fatal error: readline/readline.h: No such file or directory
- Lua错误:nesting of [[...]] is deprecated near '['
- 逻辑操作符---Lua: and,or,not 对比 C++:&&,||,!
- lua转让C++书面DLL达到“热更新”
- lua userdata
- lua-epoll 模块简单分析
- lua c API
- 协程Coroutine——用同步的方式编写异步的逻辑
- Lua
- lua package编写
- LeetCode-Evaluate Reverse Polish Notation
- lua元表
- Evaluate Reverse Polish Notation
- PB中的Describe,Evaluatate,LookUpDisplay小结
- c++调用lua方法
- c++&lua交互
- 【学习笔记】系列七:Lua 语言 15 分钟快速入门
- 【wireshark】插件开发(四):Lua插件Post-dissector和Listener