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

C++

class Solution {
public:
int evalRPN(vector<string> &tokens){
int len = tokens.size();
stack<int> S;
for (int i = 0; i< len; i++){
if ("+" == tokens[i] || "-" == tokens[i] || tokens[i] == "*" || tokens[i] == "/"){
int arg2 = S.top(); S.pop();
int arg1 = S.top(); S.pop();
S.push(runOperator(arg1,arg2,tokens[i][0]));
}else
S.push(stoi(tokens[i]));
}
return S.top();
}
int runOperator(int arg1,int arg2,char optor){
if('+' == optor) return arg1 + arg2;
else if('-' == optor) return arg1 - arg2;
else if('*' == optor) return arg1 * arg2;
else return arg1 / arg2;
}
};

evaluate-reverse-polist-notation leetcode C++的更多相关文章

  1. 150. Evaluate Reverse Polish Notation - LeetCode

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

  2. Evaluate Reverse Polish Notation——LeetCode

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

  3. Evaluate Reverse Polish Notation leetcode java

    题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...

  4. Evaluate Reverse Polish Notation --leetcode

    原题链接:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目大意:给出逆波兰式,然后求其结果. 解题方法:单个栈 ...

  5. 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 ...

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

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

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

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

  8. 【leetcode】Evaluate Reverse Polish Notation

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

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

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

  10. leetcode - [2]Evaluate Reverse Polish Notation

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

随机推荐

  1. 【优化技术专题】「温故而知新」基于Quartz系列的任务调度框架的动态化任务实现分析

    不提XXLJOB或者其他的调度框架,就看我接触的第一个任务调度框架Quartz(温故而知新) Quartz的动态暂停 恢复 修改和删除任务 实现动态添加定时任务,先来看一下我们初步要实现的目标效果图, ...

  2. Java on Visual Studio Code的更新 – 2021年8月

    Nick Senior Program Manager, Developer Division at Microsoft 大家好,欢迎来到 8 月版的 Visual Studio Code Java ...

  3. PHP中命名空间是怎样的存在?(二)

    今天带来的依然是命名空间相关的内容,本身命名空间就是PHP中非常重要的一个特性.所以关于它的各种操作和使用还是非常复杂的,光使用方式就有很多种,我们一个一个的来看. 子命名空间 命名空间本身就像目录一 ...

  4. [原创]OpenEuler20.03安装配置PostgreSQL13.4详细图文版

    OpenEuler安装配置PostgreSQL 编写时间:2021年9月18日 作者:liupp 邮箱:liupp@88.com 序号 更新内容 更新日期 更新人 1 完成第一至三章内容编辑: 202 ...

  5. 谷歌浏览器chrome安装插件报"程序包无效: CRX_HEADER_INVALID"错误

    今天参加需求评审,看到原来可以谷歌浏览器查看Axure原型文件,真是只有想不到,没有做不到(自己孤陋寡闻了,第一次接触Axure). 需求评审后,我百度"如何使用谷歌浏览器查看Axure原型 ...

  6. CI框架 core

    https://blog.csdn.net/admin_admin/article/details/51769805 1.扩展控制器 1.在application/core新建一个自己的控制器(MY_ ...

  7. eval(input())

    看到一段代码,判读输入的数字,用的是eval(input()),查了一下,原来input()会把所有输入值,包括数字,视为字符串,而eval()会去掉字符串最外层的引号,然后当做Python语句执行[ ...

  8. P5748-集合划分计数【EGF,多项式exp】

    正题 题目链接:https://www.luogu.com.cn/problem/P5748 题目大意 求将\(n\)的排列分成若干个无序非空集合的方案. 输出答案对\(998244353\)取模. ...

  9. MyBatis封装对象内的List出现的问题

    本篇文章问题1:wife的复数形式是wives,不是wifes,英语不好请见谅. 对象举例: class User { private String username; private List< ...

  10. 一、Ansible基础之入门篇

    目录 1. Ansible基础 1.1 介绍 1.2 工作原理 1.3 如何安装 1.3.1 先决条件 1.3.2 安装Ansible 1.4 管理节点与被管理节点建立SSH信任关系 1.5 快速入门 ...