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

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

说明:

  • 整数除法只保留整数部分。
  • 给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 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. 【转载】flex布局超过显示省略号

    <div class="main"> <div class="content"> <h4 class="name&quo ...

  2. python基础语法(运算符及优先级)

    python基础语法(运算符及优先级) python语言支持的运算符类型 算数运算符 假设变量a为10,变量b为21 算数符 描述 实例 + 加-两个对象相加 a+b结果31 - 减-得到一个负数或者 ...

  3. vue 返回上一页

    参考:https://www.cnblogs.com/chenguiya/p/9118265.html 注:需为history模式 方法一: @click="back" back( ...

  4. SSM三大框架的运行流程、原理、核心技术详解

    一.Spring部分1.Spring的运行流程第一步:加载配置文件ApplicationContext ac = new ClassPathXmlApplicationContext("be ...

  5. opencv-识别手写数字

    首先拆分图片得到数据 #include "stdafx.h" #include <iostream> #include "opencv2/opencv.hpp ...

  6. html-from提交表单

    使用form创建的仅仅是一个空白的表单, 我们还需要向form中添加不同的表单项 <!DOCTYPE html> <html> <head> <meta ch ...

  7. Oracle - 用户及表空间的创建和删除

    -- 查询所有用户 SELECT USERNAME FROM ALL_USERS; -- 查询所有表空间 SELECT TABLESPACE_NAME FROM USER_TABLESPACES; - ...

  8. duilib教程之duilib入门简明教程3.第一个程序 Hello World

    小伙伴们有点迫不及待了么,来看一看Hello World吧:新建一个空的win32项目,新建一个main.cpp文件,将以下代码复制进去: #include <windows.h> #in ...

  9. python的meshgrid用法和3D库 mpl_toolkits.mplot3d 与PolynomialFeatures多项式库学习

    meshgrid import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Ax ...

  10. apache配置域名子目录,访问不同子项目

    <VirtualHost *:443> DocumentRoot "E:/phpstudy/WWW/nextplus" ServerName local-main.co ...