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 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 地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
计算波兰后缀表达式的值,具体做法是利用一个栈,如果遇到数字则把数字进栈,如果遇到操作符就把栈中的数字出栈进行运算(二元操作符出栈两个数字,一元操作符出栈一个数字),最后的结果存在栈中。代码:
class Solution {
public:
int evalRPN(vector<string> &tokens) {
int lop, rop;
vector<string>::const_iterator cIter = tokens.begin();
stack<int> s;
for(; cIter != tokens.end(); ++cIter){
if ((*cIter).size() == && !isdigit((*cIter)[])){
char op = (*cIter)[];
rop = s.top();
s.pop();
lop = s.top();
s.pop();
if ('+' == op){
s.push(lop + rop);
} else if ('-' == op){
s.push(lop - rop);
} else if ('*' == op){
s.push(lop * rop);
} else{
s.push(lop / rop);
}
} else {
s.push(atoi((*cIter).c_str()));
}
}
return s.top();
}
};
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation的更多相关文章
- LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode150:Evaluate Reverse Polish Notation
题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
- HDU——1062Text Reverse(水题string::find系列+reverse)
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
随机推荐
- [irving] C# Windows Beep 调用声音文件
方法一:Console.Beep(); 方法二:可以用Console.WriteLine("/a");来代替Beep(). MSDN:http://msdn.microsoft.c ...
- div模拟的下拉框特效jquery
从网上找来的,感觉不错就拿来分享下 <style type="text/css"> body, ul, li { margin: 0; padding: 0; font ...
- Linux服务器下没有root权限装Matlab R2013a
Matlab R2013a Unix版下载地址 注意:由于我是在单位的集群系统上装Matlab,没有root权限,故下载下来的.iso文件不能在linux下用mount命令挂载,故先在Win下解压,再 ...
- fork之后发生了什么(基于3.16-rc4)
在linux c编程中,我们可以使用fork,vfork,clone三个系统调用来创建子进程.下面我们先分析下fork系统调用的实现原理.代码如下(kernel/fork.c): #ifdef __A ...
- homework-附加题:第12章基本数据类型阅读总结
基本数据类型是构建其他所有数据类型的构造块,本人认为这部分是计算机编程的基础,值得得到大家的注意. 首先,在本章中作者提到了避免使用magic number.使用magic number这种做法是极其 ...
- JavaScript如何判断参数为浮点型
在codewars里,确实可以学到很多很酷的方法,例如这一次的题目是判断数字是否为浮点型.我一开始是想有没有原生的js方法,像isNaN(),isFinite(),在前者Infinity是不属于NaN ...
- [C语言 - 2] C语言变量
A.变量的作用域: 1.局部变量:在函数或者代码块内部定义的变量 作用域:从定义处到代码块结束 生命周期:从定义处分配控件,代码块结束后被回收 局部变量没有默认值,要自己初始化 2.全局变量:在函 ...
- Flask框架获取用户IP地址的方法
本文实例讲述了python使用Flask框架获取用户IP地址的方法.分享给大家供大家参考.具体如下: 下面的代码包含了html页面和python代码,非常详细,如果你正使用Flask,也可以学习一下最 ...
- CodeForces 707C Pythagorean Triples (数论)
题意:给定一个数n,问你其他两边,能够组成直角三角形. 析:这是一个数论题. 如果 n 是奇数,那么那两边就是 (n*n-1)/2 和 (n*n+1)/2. 如果 n 是偶数,那么那两边就是 (n/2 ...
- js隐藏
function openLoadingIcon(){ $("#dataLoading").css("display","block"); ...