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

使用栈(Stack),如果是操作数则压栈,如果是操作符则弹出栈顶两个元素进行相应运算之后再压栈。

最后栈中剩余元素即计算结果。

注意:操作数的顺序。

class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> stk;
for(int i = ; i < tokens.size(); i ++)
{
if(tokens[i] == "+")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b+a);
}
else if(tokens[i] == "-")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b-a);
}
else if(tokens[i] == "*")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b*a);
}
else if(tokens[i] == "/")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b/a);
}
else
{
int digit = atoi(tokens[i].c_str());
stk.push(digit);
}
}
return stk.top();
}
};

【LeetCode】150. Evaluate Reverse Polish Notation的更多相关文章

  1. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

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

  3. LeetCode OJ 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 - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

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

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

  6. Java for LeetCode 150 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 ------ java

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

  8. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

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

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

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

随机推荐

  1. Ubuntu 将应用程序固定到快速启动栏

    Ubuntu上没有快捷方式的说法,而通过软件中心安装的软件就有图标,并能加入到启动器上,这是因为它们有一个desktop配置文件的缘故.这些配置文件在/usr/share/applications这个 ...

  2. Python 基础学习 总结篇

    Python 基础学习总结 先附上所有的章节: Python学习(一)安装.环境配置及IDE推荐 Python学习(二)Python 简介 Python学习(三)流程控制 Python学习(四)数据结 ...

  3. 使用java api操作HDFS文件

    实现的代码如下: import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import ...

  4. 数学图形之Breather surface

    这是一种挺漂亮的曲面图形,可惜没有找到太多的相关解释. In differential equations, a breather surface is a mathematical surface ...

  5. Java JDBC数据库链接

    好久没有编写有关数据库应用程序啦,这里回顾一下java JDBC. 1.使用Java JDBC操作数据库一般需要6步: (1)建立JDBC桥接器,加载数据库驱动: (2)连接数据库,获得Connect ...

  6. poj 3131 Cubic Eight-Puzzle 双向广搜 Hash判重

    挺不错的题目,很锻炼代码能力和调试能力~ 题意:初始格子状态固定,给你移动后格子的状态,问最少需要多少步能到达,如果步数大于30,输出-1. 由于单向搜索状态太多,搜到二十几就会爆了,所以应该想到双向 ...

  7. Maximum Depth of Binary Tree leetcode java

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  8. 可进可退,jQuery图片、视频、flash播放插件prettyPhoto使用记录

    一.prettyPhoto简介 prettyPhoto是一款基于jquery的轻量级的lightbox图片播放浏览插件,它不仅支持图片,还同时支持视频.flash.YouTube.iframe和aja ...

  9. 如何在Linux中发现IP地址冲突

    导读 你们都知道什么是IP地址,是吧?它们被分配给网络上的设备来代表它们.它们通过DHCP服务器分配并且会经常改变.现在有两种IP地址.动态的一种会经常改变(几天一次),而静态的就如它的名字那样是静态 ...

  10. 使用Gnupg对Linux系统中的文件进行加密

    GnuPG(GNU Privacy Guard或GPG)是一个以GNU通用公共许可证释出的开放源码用于加密或签名的软件,可用来取代PGP.大多数gpg软件仅支持命令行方式,一般人较难掌握.由于gpg软 ...