【LeetCode练习题】Evaluate Reverse Polish Notation
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的更多相关文章
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
- Leetcode#150 Evaluate Reverse Polish Notation
原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...
随机推荐
- django项目环境搭建备忘
由于使用python3,所以尽量为每个项目配置虚拟环境来管理各个项目的=. 新建一个项目文件夹,进入该路径 python3 -m venv ll_env 然后激活虚拟环境 source ll_env/ ...
- ObjectOutputStream 追加写入读取错误 - 自己的实现方案
本篇博客灵感来自http://blog.csdn.net/chenssy/article/details/13170015 问题描述.问题出现的原因.尝试解决办法,请参见鄙人上一编博客. 上一编文章解 ...
- Vitaliy and Pie(模拟)
Vitaliy and Pie Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
- JVM工作原理和特点(一些二逼的逼神面试官会问的问题)
作为一种阅读的方式了解下jvm的工作原理 ps:(一些二逼的逼神面试官会问的问题) JVM工作原理和特点主要是指操作系统装入JVM是通过jdk中Java.exe来完毕,通过以下4步来完毕JVM环境. ...
- Chord算法实现具体
背景 Chord算法是DHT(Distributed Hash Table)的一种经典实现.下面从网上无节操盗了一段介绍性文字: Chord是最简单.最精确的环形P2P模型."Chord&q ...
- cocos2d-x结合cocosbuilder,不同屏幕适配小结
这个问题搞了好几天才解决,在此总结一下: 首先约定只使用一套图片资源同时应用于iphon4和iphon5(测试过在ipad下也能显示正常), 这里我们将需要全屏显示的背景制作为iphon5的尺寸即:1 ...
- Unity NGUI 血条制作
NGUI 血条制作步骤 实现过程: 模拟血条的变化当点击按钮Button是血条会实时发生变化. 1.向Unity中导入NGUI2.6.3.unitypackage 点击create your ui 后 ...
- XHR 框架与 Dojo( xhrGet,xhrPut,xhrDelete)
总结 本文介绍了 Dojo 中三种浏览器与服务器交互的方式,这三种方式各有优缺点,但是在使用方式却出奇的一致: xhr 框架的函数,dojo.io.iframe.dojo.io.script 对象的函 ...
- ThinkPHP框架搭建及常见问题(Apache或MySQL无法启动)----简单的初体验
有一定基础的人勿进,这篇讲的只是零基础入门,都是我刚接触以及我所了解到的人刚开始有疑惑的地方,具体框架介绍会在后面的博客中介绍 这一篇只是为了一个简单的页面显示而介绍的方法,不涉及代码,开发环境,所以 ...
- [Math]Reverse Integer
Total Accepted: 111287 Total Submissions: 474471 Difficulty: Easy Reverse digits of an integer. Exam ...