首页 > 其他分享 >[Google] LeetCode 150 Evaluate Reverse Polish Notation

[Google] LeetCode 150 Evaluate Reverse Polish Notation

时间:2022-09-01 16:25:09浏览次数:57  
标签:150 Google Reverse Notation int Evaluate Polish expression

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

Valid operators are +, -, *, and /. Each operand may be an integer or another expression.

Note that division between two integers should truncate toward zero.

It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.

Solution

直接用 \(stack\) 维护即可

点击查看代码
class Solution {
private:
    stack<int> s;
    
    bool check_opt(string s){
        if(s=="+"|| s=="-"||s=="*"||s=="/")return true;
        return false;
    }
    
public:
    int evalRPN(vector<string>& tokens) {
        int n = tokens.size();
        for(int i=0;i<n;i++){
            if(check_opt(tokens[i])){
                //cout<<s.top()<<endl;
                int num1 = (s.top());s.pop();
                int num2 = (s.top());s.pop();
                string tmp = tokens[i];
                if(tmp=="+")s.push(num1+num2);
                else if(tmp=="-")s.push(num2-num1);
                else if(tmp=="*")s.push(num1*num2);
                else s.push(num2/num1);
            }
            else{
                s.push(stoi(tokens[i]));
            }
        }
        return s.top();
    }
};

标签:150,Google,Reverse,Notation,int,Evaluate,Polish,expression
From: https://www.cnblogs.com/xinyu04/p/16646895.html

相关文章

  • [Google] LeetCode 552 Student Attendance Record II
    Anattendancerecordforastudentcanberepresentedasastringwhereeachcharactersignifieswhetherthestudentwasabsent,late,orpresentonthatday.......
  • [Google] LeetCode 489 Robot Room Cleaner
    Youarecontrollingarobotthatislocatedsomewhereinaroom.Theroomismodeledasanmxnbinarygridwhere0representsawalland1representsanempt......
  • CCF 201503-1 图像旋转(C++)
    好像旋转矩阵有更好的做法,但是我觉得这样也足够了,如果需要更好的做法,大家得自己在去找一下。我主要是找了下规律,然后做出来的#include<iostream>#include<bits/stdc+......
  • Google Chrome谷歌浏览器离完整离线安装包下载地址整理总汇
    每次重装系统,都要为安装Chrome而烦恼。虽然现在可以直接从谷歌浏览器官网下载在线安装包进行安装,但是在线安装包安装的版本不可控,大概率是x86版本,而且在断网状态下也......
  • [Google] LeetCode 715 Range Module 线段树
    ARangeModuleisamodulethattracksrangesofnumbers.Designadatastructuretotracktherangesrepresentedashalf-openintervalsandqueryaboutthem.......
  • [CTF]2022 CNSS夏令营 Web&Reverse 复现wp
    写在前面很有趣的一次(大)学前教育,作为一个22级泥电新生,对CTF网安类的东西完全是一窍不通,进新生群然后就被拉进来Van了.结果没想到还挺好玩(可能是我这几天太无聊......
  • 在 Google Colab 中运行 Selenium WebDriver
    在GoogleColab中运行SeleniumWebDriverPhotoby克里斯·里德on不飞溅如果您需要在GoogleColab中为您的分析项目抓取数据,则无需事先构建单独的网络抓取工具......
  • [Google] LeetCode 2128 Remove All Ones With Row and Column Flips
    Youaregivenanmxnbinarymatrixgrid.Inoneoperation,youcanchooseanyroworcolumnandflipeachvalueinthatroworcolumn(i.e.,changingall0's......
  • googletest总结
    目录前言googletest简介参考前言使用常用的googletest脚本,编写出易用的,可维护的业务代码贯穿TDD的思想。googletest简介参考googletestusergide......
  • Google C++ Style Guide 学习
    目录参考参考http://home.ustc.edu.cn/~hqp/RootClass/AddFiles2/GoogleC++StyleGuide.pdfhttps://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styl......