题目:

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

解题思路:

很简单的一题,直接利用栈实现,不多说了

实现代码:

#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std; /*
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
*/
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<string> operand_stack;
vector<string>::iterator iter;
for(iter = tokens.begin(); iter != tokens.end(); ++iter)
{
if(*iter == "+")//这里就不提取重复代码了,直接简单点
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
//int ret = atoi(op2.c_str()) + atoi(op1.c_str());
int ret = stoi(op2) + stoi(op1);//stoi为C++11才有
string result = to_string(ret);
operand_stack.push(result); }
else if(*iter == "-")
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
int ret = atoi(op2.c_str()) - atoi(op1.c_str());
string result = to_string(ret);
operand_stack.push(result); }
else if(*iter == "*")
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
int ret = atoi(op2.c_str()) * atoi(op1.c_str());
string result = to_string(ret);
operand_stack.push(result); }
else if(*iter == "/")
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
if( atoi(op1.c_str()) == )
return 0x7FFFFFF;
int ret = atoi(op2.c_str()) / atoi(op1.c_str());
string result = to_string(ret);
operand_stack.push(result); }
else
operand_stack.push(*iter);
}
return atoi(operand_stack.top().c_str()); } }; int main(void)
{
string strs[] = {"", "", "", "/", "+"};
vector<string> tokens(strs, strs+);
Solution solution;
int ret = solution.evalRPN(tokens);
cout<<ret<<endl;
return ;
}

LeetCode150:Evaluate Reverse Polish Notation的更多相关文章

  1. 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 arithm ...

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

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

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

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

  4. 【leetcode】Evaluate Reverse Polish Notation

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

  5. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

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

  6. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  7. leetcode - [2]Evaluate Reverse Polish Notation

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

  8. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  9. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

随机推荐

  1. RN中关于组件中属性的传递

    比如: 组件A想要给组件B中的组件C传递一个属性prop class A extends Component{ render(){ return( <B title = "这是一个标题 ...

  2. DP解LCS问题模板及其优化

    LCS--Longest Common Subsequence,即最长公共子序列,一般使用DP来解. 常规方法: dp[i][j]表示字符串s1前i个字符组成的字符串与s2前j个字符组成的字符串的LC ...

  3. C++ auto

    auto用来声明自动变量.它是存储类型标识符,表明变量(自动)具有本地范围.块范围的变量声明(如for循环体内的变量声明)默认为auto存储类型. 好处:auto变量在离开作用域是会变程序自动释放,不 ...

  4. The Unique MST

    The Unique MST http://poj.org/problem?id=1679 Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  5. swift - tableview 滚动到指定位置

    滚动一定要在  tableView.reloadData()之后进行 1. 默认  plain 模式 办法1. tableView.contentOffset.y = 0 办法2 tableView. ...

  6. jquery之DataTables的使用

      jquery之DataTables的使用  document jquery function lsquo 强大的表格解决方案,有多强大,一起来看下吧: 1.DataTables的默认配置 $(do ...

  7. js Array Map and Set

    Array slice slice()就是对应String的substring()版本,它截取Array的部分元素,然后返回一个新的Array: var arr = ['A', 'B', 'C', ' ...

  8. linux c++连接mysql编译问题

  9. Jmeter参数化HTTP request中Send Files With The Request的文件路径和文件名

  10. linux 硬盘分区攻略

    以下的sdX代表硬盘分区(如sda1,sda2,sdb1...等等),如果已有的硬盘分区需要改变大小的话,请参考另一篇文章. /boot:开机用的磁盘空间了,至少78MB,一般给100MB就好了. / ...