Leetcode—1396. 设计地铁系统【中等】

2024每日刷题(127)

Leetcode—1396. 设计地铁系统

在这里插入图片描述

实现代码

class UndergroundSystem {
public:
    typedef struct Checkin {
        string startStation;
        int time;
    } Checkin;

    typedef struct Checkout{
        int tripNum;
        int totalTime;
    } Checkout;

    UndergroundSystem() {

    }
    
    void checkIn(int id, string stationName, int t) {
        CheckinPerson[id] = {stationName, t};
    }
    
    void checkOut(int id, string stationName, int t) {
        auto [startStation, time] = CheckinPerson[id];
        string name = startStation + "->" + stationName;
        stationInfo[name].tripNum++;
        stationInfo[name].totalTime += t - time;
        CheckinPerson.erase(id);
    }
    
    double getAverageTime(string startStation, string endStation) {
        auto [tripNum, totalTime] = stationInfo[startStation + "->" + endStation];
        if(tripNum == 0) {
            return 0;
        } 
        return totalTime / (double)tripNum;
    }
private:
    unordered_map<int, Checkin> CheckinPerson;
    unordered_map<string, Checkout> stationInfo;
};

/**
 * Your UndergroundSystem object will be instantiated and called as such:
 * UndergroundSystem* obj = new UndergroundSystem();
 * obj->checkIn(id,stationName,t);
 * obj->checkOut(id,stationName,t);
 * double param_3 = obj->getAverageTime(startStation,endStation);
 */

运行结果

在这里插入图片描述
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐

  1. leetcode139-Word Break

    2024-05-09 18:16:05       5 阅读
  2. Leetcode 139 单词拆分

    2024-05-09 18:16:05       28 阅读
  3. leetcode 139. 单词拆分

    2024-05-09 18:16:05       20 阅读
  4. LeetCode 139. 单词拆分

    2024-05-09 18:16:05       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-09 18:16:05       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-09 18:16:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-09 18:16:05       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-09 18:16:05       20 阅读

热门阅读

  1. C# Solidworks二次开发:枚举应用实战(第十二讲)

    2024-05-09 18:16:05       11 阅读
  2. 学习c#第23天 StringBuilder 效率测试

    2024-05-09 18:16:05       14 阅读
  3. LeetCode 16.最接近的三数之和

    2024-05-09 18:16:05       16 阅读
  4. thinkphp5.1 新建模块

    2024-05-09 18:16:05       12 阅读
  5. C++关联容器unordered_map无法通过索引来访问元素

    2024-05-09 18:16:05       11 阅读
  6. 05. 基于Verilog的呼吸灯程序设计

    2024-05-09 18:16:05       14 阅读
  7. numpy常用方法

    2024-05-09 18:16:05       11 阅读
  8. filebeat处理k8s docker模式下部署导致deviceid改变

    2024-05-09 18:16:05       12 阅读