根据逆波兰表示法,求表达式的值。

有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。

说明:

  • 整数除法只保留整数部分。
  • 给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。

示例 1:

输入: ["2", "1", "+", "3", "*"] 输出: 9 解释: ((2 + 1) * 3) = 9

示例 2:

输入: ["4", "13", "5", "/", "+"] 输出: 6 解释: (4 + (13 / 5)) = 6

示例 3:

输入: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] 输出: 22 解释: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 = ((10 * (6 / (12 * -11))) + 17) + 5 = ((10 * (6 / -132)) + 17) + 5 = ((10 * 0) + 17) + 5 = (0 + 17) + 5 = 17 + 5 = 22

class Solution {
public:
int evalRPN(vector<string>& tokens)
{
stack<int> s;
for(int i = 0; i < tokens.size(); i++)
{
if(tokens[i] == "+")
{
int num1 = s.top();
s.pop();
int num2 = s.top();
s.pop();
s.push(num1 + num2);
}
else if(tokens[i] == "-")
{
int num1 = s.top();
s.pop();
int num2 = s.top();
s.pop();
s.push(num2 - num1);
}
else if(tokens[i] == "*")
{
int num1 = s.top();
s.pop();
int num2 = s.top();
s.pop();
s.push(num1 * num2);
}
else if(tokens[i] == "/")
{
int num1 = s.top();
s.pop();
int num2 = s.top();
s.pop();
s.push(num2 / num1);
}
else
{
int j = 0;
int temp = 0;
bool isPositive = true;
if(tokens[i][j] == '-')
{
j++;
isPositive = false;
}
for(; j < tokens[i].size(); j++)
{
temp = temp * 10 + (tokens[i][j] - '0');
}
s.push(isPositive == true? temp : -temp);
}
}
return s.top();
}
};

Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值的更多相关文章

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

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

  2. 150 Evaluate Reverse Polish Notation 逆波兰表达式求值

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

  3. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  4. 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(逆波兰式的计算)

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

  6. 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逆波兰表示法

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

  8. Java Evaluate Reverse Polish Notation(逆波兰式)

    表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...

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

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

随机推荐

  1. 将近半个小时,把一小段简短的汇编代码写成了C语言代码

    我自己看,感觉好像一句一句翻译的,写得很是生硬,不如书上写的灵活 0040137E    8B7424 04       MOV ESI,DWORD PTR SS:[ESP+4]00401382    ...

  2. UNIX环境高级编程------apue.h找不到

    运行1-3代码时,出现问题:apue.h 没有找到问题 1.去此网址下载源码: http://www.apuebook.com/code3e.html 压缩包名为:src.3e.tar.gz 2.解压 ...

  3. C++ 系列:extern

    extern 作用1:声明外部变量现代编译器一般采用按文件编译的方式,因此在编译时,各个文件中定义的全局变量是互相透明的,也就是说,在编译时,全局变量的可见域限制在文件内部. 例1:创建一个工程,里面 ...

  4. Android开发 EditText按回车按键后出现 focus search returned a view that wasn't able to take focus! 错误

    问题描述 将EditText这个View成为了ListView或者RecyclerView的item时,在按输入法的回车/下一步/next时会出现的 focus search returned a v ...

  5. Servlet3.0要点小结

    1. 注解配置Servlet @WebServlet name属性: servlet名称 value属性或urlPatterns属性: servlet映射路径, 可配置多个 initParams属性: ...

  6. LUOGU P1453 城市环路(基环树+dp)

    传送门 解题思路 一道基环树上$dp$的题,这种题比较套路吧,首先第一遍$dfs$把环找出来,然后对于环上的每一个点都向它子树内做一次树形$dp$,$f[i][0/1]$表示到了$i$这个点选或不选的 ...

  7. Android基础控件ImageView的使用

    1.相关属性 <!--src 设置内容--> <!--background 设置背景--> <!--alpha 设置透明度 --> <!--adjustVie ...

  8. 服务器迁移部署PosApp

    绑定 基本配置 高级设置

  9. FreeMarker 自定义 TemplateDirectiveModel(二)

    FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker 与 Web 容器无关,即在 Web 运行时,它并不知道 Servlet 或 HTTP.它不 ...

  10. 【JEECG-Boot 技术文档】新手入门教程

    Jeecg-Boot入门教程必看(新手学习) 1.开发环境搭建 http://jeecg-boot.mydoc.io/?t=345670 开发工具 :https://pan.baidu.com/sha ...