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的更多相关文章

  1. LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)

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

  2. LeetCode150:Evaluate Reverse Polish Notation

    题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...

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

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

  4. lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

    题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...

  5. HDU——1062Text Reverse(水题string::find系列+reverse)

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  6. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  7. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Apache OFBiz 学习笔记 之 服务引擎 一

    概述     服务定义为一段独立的逻辑顺序,当多个服务组合一起时可完成不同类型的业务需求     服务有很多类型,WorkFlow.Rules.Java.SOAP.BeanShell等.java类型的 ...

  2. <转>Python 多线程的单cpu与cpu上的多线程的区别

    你对Python 多线程有所了解的话.那么你对python 多线程在单cpu意义上的多线程与多cpu上的多线程有着本质的区别,如果你对Python 多线程的相关知识想有更多的了解,你就可以浏览我们的文 ...

  3. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_labels(self, locator)

    def get_selected_list_labels(self, locator): """Returns the visible labels of selecte ...

  4. MFC特定函数的应用20160720(SystemParametersInfo,GetWindowRect,WriteProfileString,GetSystemMetrics)

    1.SystemParametersInfo函数可以获取和设置数量众多的windows系统参数 MFC中可以用 SystemParametersInfo(……) 函数来获取和设置系统信息,如下面例子所 ...

  5. 进程通信之一 使用WM_COPYDATA C++及C#实现(转)

    进程间通信最简单的方式就是发送WM_COPYDATA消息.本文提供C++及C#程序相互通信的二种实现方式.这样消息的接收端可以用C++实现,发送端可以用C++或C#实现.     发送WM_COPYD ...

  6. C++单例模板

    #pragma once namespace MyGame { template<typename T> class Global { public: static void Create ...

  7. DAG的生成

    DAG的生成 原始的RDD(s)通过一系列转换就形成了DAG.RDD之间的依赖关系,包含了RDD由哪些Parent RDD(s)转换而来和它依赖parent RDD(s)的哪些Partitions,是 ...

  8. Android实例-获取安卓手机WIFI信息(XE8+小米2)

    结果: 1.必须打开Access wifi state权限,不打开权限会出图二的错误. 相关资料: http://blog.csdn.net/lyf_lyf/article/category/1735 ...

  9. 从 ALAsset 中取出属性

    #pragma mark - 从相册数组中取出所有的 图片数据 -(NSMutableArray *)getImageFromAlbumArray:(NSArray *)albumArr { NSMu ...

  10. CodeForces 548A Mike and Fax (回文,水题)

    题意:给定一个字符串,问是不是恰好存在 k 个字符串是回文串,并且一样长. 析:没什么好说的,每次截取n/k个,判断是不是回文就好. 代码如下: #include<bits/stdc++.h&g ...