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

本文介绍了如何在LeetCode题目1396中实现地铁系统类,包括checkIn、checkOut方法以及getAverageTime函数的使用。作者提供了类的完整实现,并分享了运行结果,鼓励读者点赞关注支持。
摘要由CSDN通过智能技术生成

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);
 */

运行结果

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值