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
 
题目意思:
计算出波兰计数法的结果。 解题思路:
利用一个栈,遇到数字就压栈,遇到符号就弹出两个数字,计算结果再push到栈里去。
注意几个函数就行了:isdigit()、stoi()。 代码如下:
 class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> ret;
int len = tokens.size();
for(int i = ; i < len; i++){
if( isdigit(tokens[i][]) || tokens[i].size() > ){
//如果是负数,第一个就是符号。
ret.push(stoi(tokens[i]));
}
else{
int op2 = ret.top();
ret.pop();
int op1 = ret.top();
ret.pop(); //注意除法和减法顺序,是用后pop出来的减去先pop出来的
switch(tokens[i][]){
case '+':
ret.push(op1 + op2);
break;
case '-':
ret.push(op1 - op2);
break;
case '*':
ret.push(op1 * op2);
break;
case '/':
ret.push(op1 / op2);
break;
}
}
}
return ret.top();
}
};
												

【LeetCode练习题】Evaluate Reverse Polish Notation的更多相关文章

  1. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  2. leetcode - [2]Evaluate Reverse Polish Notation

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

  3. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

  4. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

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

  5. 【leetcode】Evaluate Reverse Polish Notation(middle)

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

  6. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

  7. leetcode 150. Evaluate Reverse Polish Notation ------ java

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

  8. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

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

  9. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

  10. Leetcode#150 Evaluate Reverse Polish Notation

    原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...

随机推荐

  1. 常用数据结构及复杂度 array、LinkedList、List、Stack、Queue、Dictionary、SortedDictionary、HashSet、SortedSet

    原文地址:http://www.cnblogs.com/gaochundong/p/data_structures_and_asymptotic_analysis.html  常用数据结构的时间复杂度 ...

  2. 回归分析:非线性nlinfi

    今天测试.这首题,真的很头疼,第一次看到这个题,就知道要用nlinefit函数做,但是我一个地方没搞清楚, 花了我40多分钟还没做也来. 最后终于是调用的函数出错了.主要是没有将一个列抽出来.一个二维 ...

  3. convertView

    [convertView] 参考:https://zhidao.baidu.com/question/423895201122905772.html

  4. UIView 弹出动画

    // 展开动画 - (void)beginAnimations { CGContextRef context = UIGraphicsGetCurrentContext(); [UIView begi ...

  5. 了解XSS攻击

    XSS又称CSS,全称Cross SiteScript,跨站脚本攻击,是Web程序中常见的漏洞,XSS属于被动式且用于客户端的攻击方式,所以容易被忽略其危害性.其原理是攻击者向有 XSS漏洞的网站中输 ...

  6. 执行此安装程序之前,必须安装 32 位 Windows 映像处理组件(WIC)解决的方法

    我们在Windows Service 2003上安装 Microsoft .NET Framework4.0时常常出现以下的报错 执行此安装程序之前,必须安装 32 位 Windows 映像处理组件( ...

  7. 第五章 Logistic回归

    第五章 Logistic回归 假设现在有一些数据点,我们利用一条直线对这些点进行拟合(该线称为最佳拟合直线),这个拟合过程就称作回归. 为了实现Logistic回归分类器,我们可以在每个特征上都乘以一 ...

  8. winform —— 常用控件

    1.textbox: 属性:text:    文本selectedtext:         获或设置选中文本canundo:         是否能够撤销 passwordchar:替换字符实现密码 ...

  9. Godaddy主机从购买到开通的详细图文教程(2013年)

    http://bbs.zhujiusa.com/thread-10-1-1.html Godaddy主机从购买到开通的详细图文教程(2013年最新) Godaddy是全球域名注册商中的NO.1,同时也 ...

  10. dojo.declare

    参考:http://www.ibm.com/developerworks/cn/web/1203_xiejj_dojodeclare/