Total Accepted: 55722 Total Submissions: 249668 Difficulty: Medium

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) {
int tokens_size = tokens.size();
int i=,k=;
long long int num1 =,num2=;
while(i<tokens_size){
if(tokens[i]=="+" || tokens[i]=="-" || tokens[i]=="*" ||tokens[i]=="/"){
num2 = stoi(tokens[--k]);
num1 = stoi(tokens[--k]);
switch(tokens[i][]){
case '+': num1+=num2;break;
case '-': num1-=num2;break;
case '*': num1*=num2;break;
case '/': num1/=num2;break;
}
tokens[k++] = to_string(num1);
i++;
}else{
tokens[k++]=tokens[i++];
}
}
return stoi(tokens[]);
}
};

[stack]Evaluate Reverse Polish Notation的更多相关文章

  1. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  2. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  3. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  4. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  5. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  6. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  7. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

  8. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  9. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

随机推荐

  1. MySQL Update 使用

    备忘: USE `xxx`; ; UPDATE `TB_MB_1` T SET T.`MedicalCount` = ( SELECT S.Total-- ,S.`HospitalID` FROM( ...

  2. XML.ObjTree -- XML source code from/to JavaScript object like E4X

    转载于:http://www.kawa.net/works/js/xml/objtree-try-e.html // ========================================= ...

  3. 使用一个HttpModule拦截Http请求,来检测页面刷新(F5或正常的请求)

    在Web Application中,有个问题就是:“我怎么来判断一个http请求到底是通过按F5刷新的请求还是正常的提交请求?” 相信了解ASP.NET的人知道我在说什么,会有同感,而且这其实不是一个 ...

  4. How to Avoid Producing Legacy Code at the Speed of Typing

    英语不好翻译很烂.英语好的去看原文. About the Author I am a software architect/developer/programmer.I have a rather p ...

  5. 一个完整的SSL连接建立过程

    客户端浏览器连接到Web服务器,发出建立安全连接通道的请求. 服务器接受客户端请求,发送服务器证书做为响应. 客户端验证服务器证书的有效性,如果验证通过,则用服务器证书中包含的服务器公钥加密一个会话密 ...

  6. RMAN之一:快速入门

    1.数据导出基础 (1)创建datapump导出文件的目录对象并为相应用户授予权限. 出于安全考虑,不允许oracle用户直接在OS上进行文件的操作,而应通过directory对象指定. SQL> ...

  7. windows系统部署discuz并和javaweb账号连通同步

    一.Discuz安装说明 1.安装wamp集成环境 (1)下载wampserver集成环境 网址:(http://wampserver-64bit.en.softonic.com)或百度搜索下载 (2 ...

  8. php 字符串

    <?php /* * 字符串输出 * echo() 输出多个或多个字符串 * print() 输出字符串 * printf()格式化输出字符串 * 字符串截取 * substr() 对字符进行指 ...

  9. A+B II

    Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...

  10. IPicture总结

    1.利用IPicture接口加载.显示图片 IPicture接口管理一个图片对象和它的属性.图片对象提供对Bitmap Icon Metafile的语言不相关的抽象支持.图像对象的主要接口是IPict ...