用两个哈希表来记录所有的$id$的入站出站时间,第一个哈希表记录某$id$入站时间,第二个哈希表统计
起始站到终点站的总入站时间,每次$checkout$一个$id$的时候,在第一个哈希表中删除这个$id$,防止
哈希表过大
class UndergroundSystem {
public:
unordered_map<int,pair<string,int>>hash1;
unordered_map<string,pair<double,int>>hash2;
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
hash1[id]={stationName,t};
}
void checkOut(int id, string stationName, int t) {
auto temp=hash1[id];
auto str=temp.first+" "+stationName;
hash2[str].first+=t-temp.second;
hash2[str].second+=1;
hash1.erase(id);
}
double getAverageTime(string startStation, string endStation) {
auto str=startStation+" "+endStation;
return hash2[str].first/hash2[str].second;
}
};
/**
* 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);
*/