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. Fragment销毁时replace和add两个方法的区别

    这个首先从一个bug说起,如图:   我们都知道fragment切换有两种方式: 1. replace方式 transaction.replace(R.id.content, IndexFragmen ...

  2. Android APP开发需求文档范本

    Android  APP开发需求文档范本 软件需求文档格式的标准写法 1.引言 1.1 编写目的 • 阐明开发本软件的目的: 1.2 项目背景 • 标识待开发软件产品的名称.代码: • 列出本项目的任 ...

  3. vmware 网络连接

    解决VMware nat service等服务不能启动 虚拟机如何设置网络连接来上网?

  4. Jquery时间验证和转换工具

    var TimeObjectUtil; /** * @title 时间工具类 * @note 本类一律违规验证返回false * @author {boonyachengdu@gmail.com} * ...

  5. 不可或缺的企业OA面临问题,以及解决建议 软件定制开发 森普演示平台

    ---恢复内容开始--- 随着信息时代的来临,企业管理也相应的信息化,各种管理软件相继而出,各行各业的信息化有过成功,也有过失败(注:是以该项目是否达到用户的预期目标而言).据统计在信息化失败的案例中 ...

  6. Python中的深浅拷贝,赋值及引用

    简单来说,若对象a中存的是列表或字典等可变对象,b对a的浅拷贝只是对对象第一层的复制,修改b第二层的元素仍然会影响两个对象. 深拷贝则是不会影响原来的对象. import copy.copy() 浅拷 ...

  7. IOS 调用拨打电话Api

    // 判断设备是否有通话功能 NSString *deviceType = [UIDevice currentDevice].model; if([deviceType isEqualToString ...

  8. apache启动问题: Could not reliably determine the server's fully qualified domain name

    [root@rusky]# service httpd startStarting httpd: httpd: apr_sockaddr_info_get() failed for ruskyhttp ...

  9. 去除input在谷歌下的focus效果

    input:focus{outline: -webkit-focus-ring-color auto 0;}

  10. 仅当使用了列的列表 并且 identity_insert 为 on 时 才能在表 中为标识列指定显式值

    当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'products' 中的标识列插入显式值.” 示例: 1.首先建立一个有标识列的表:CREATE TABLE products (i ...