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. Linux命令缩写来由

    A 命令 全称 功能 备注 apt AdvancedPackaging Tool APT用来自动下载,配置,安装二进制或者源代码格式的软件包   awk Aho Weiberger and Kerni ...

  2. 谁能用通俗的语言解释一下什么是 RPC 框架

    转载自知乎:https://www.zhihu.com/question/25536695 知乎上很多问题的答案还是很好的,R大就经常在上面回答问题~ 谁能用通俗的语言解释一下什么是 RPC 框架? ...

  3. [leetcode]Flatten Binary Tree to Linked List @ Python

    原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, fl ...

  4. Bootstrap学习js插件篇之滚动监听

    1.滚动监听 案例 滚动监听插件可以根据滚动条的位置自动更新所对应的导航标记.Bootstrap中文网左侧就是一个滚动监听的例子. 代码段: <nav id="navbar-examp ...

  5. java根据经纬度获取地址

    public class GetLocation { public static void main(String[] args) { // lat 39.97646 //log 116.3039 S ...

  6. Linux kernel模块管理相关详解

    Linux内核模块化设计 1. Linux内核设计:单内核.模块化(动态装载和卸载) (1) Linux:单内核设计,但充分借鉴了微内核体系的设计的优点:为内核引入了模块化机制:(2) 内核的组成部分 ...

  7. typescript 的一种引入文件的方式 Triple-Slash Directives

    ---恢复内容开始--- /// reference 原文: https://www.typescriptlang.org/docs/handbook/triple-slash-directives. ...

  8. Mali GPU OpenGL ES 应用性能优化--測试+定位+优化流程

    1. 使用DS-5 Streamline定位瓶颈 DS-5 Streamline要求GPU驱动启用性能測试,在Mali GPU驱动中激活性能測试对性能影响微不足道. 1.1 DS-5 Streamli ...

  9. swift学习之元组

    元组在oc中是没有的.在swift中是新加的,学oc数组概念时还在想既然能够存储同样类型的元素,那不同类型的元素有没有东西存储呢,答案非常悲伤,oc没有元组这个概念.只是swift中加入了这个东西,也 ...

  10. $ionicModal

    Ionic中[弹出式窗口]有两种(如下图所示),$ionicModal和$ionicPopup; $ionicModal是完整的页面: $ionicPopup是(Dialog)对话框样式的,直接用Ja ...