[leetcode] Evaluate Reverse Polish Notation
来源:互联网 发布:淘宝客佣金代扣款给谁 编辑:程序博客网 时间:2024/06/05 11:55
From : https://leetcode.com/problems/evaluate-reverse-polish-notation/
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 resolve(string s) { int num=0, size=s.size(), flag=1; for(int i=0; i<size; i++) { if(s[i]=='-') flag=-1; if(s[i]>='0' && s[i]<='9') { num = num*10 + s[i]-'0'; } } return flag*num; } int evalRPN(vector<string>& tokens) { string cur; stack<int> nums; int size=tokens.size(); for(int i=0; i<size; i++) { cur = tokens[i]; if(cur=="+" || cur=="-" || cur=="*" || cur=="/") { ////b . a int a = nums.top(); nums.pop(); int b = nums.top(); nums.pop(); if(cur == "+") { nums.push(b+a); } else if(cur == "-") { nums.push(b-a); } else if(cur == "*") { nums.push(b*a); } else /*if(a!=0)*/{ nums.push(b/a); } } else { nums.push(resolve(cur)); } } return nums.top(); }};
0 0
- Evaluate Reverse Polish Notation | leetcode
- leetcode-Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode - Evaluate Reverse Polish Notation
- LeetCode | Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation (LeetCode)
- leetcode: Evaluate Reverse Polish Notation
- Leetcode Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation -- LeetCode
- 【LeetCode】Evaluate Reverse Polish Notation
- LeetCode-Evaluate Reverse Polish Notation
- LeetCode:Evaluate Reverse Polish Notation
- LeetCode Evaluate Reverse Polish Notation
- Leetcode:Evaluate Reverse Polish Notation
- 采用新的方法解决extjs4.1 grid合并单元格问题
- FaceBook/infer-安装说明
- odoo(Openerp v8)数据模型
- HttpURLConnection(四)-多线程下载
- 表达式计算的代码实现,按照前面两篇文章的逻辑步骤实现
- [leetcode] Evaluate Reverse Polish Notation
- UNIX环境高级编程学习笔记(六)标准I/O库
- Android App的启动过程
- DataSnap服务器如何得到客户端的IP和端口
- 【MIG专项测试组】如何准确评测Android应用的流畅度?
- js里面的属性和方法类型
- SSL基础
- 黑马程序员--数组常用排序方法
- java门禁系统面向对象程序设计