【题目描述】
https://leetcode.cn/problems/design-underground-system/
【示例】
【题目描述】admin
测试用例 | 48 / 57 |
思路描述 | 这个题目是需要我们做一个地铁管理系统, 有乘客信息, 进站的名字还有进站/出站的时间,这个时候可以定义一个类 |
不足之处 | |
import java.util.*;
// 2023-8-19
class Event {
public int id;
public String name;
public int time;
public Event(int id, String name, int time){
this.id = id;
this.name = name;
this.time = time;
}
}
class StartEnd {
public String start;
public String end;
public int cost;
public StartEnd(String start, String end, int cost){
this.start = start;
this.end = end;
this.cost = cost;
}
}
class UndergroundSystem {
Map<Integer,Event> map;
Map<Integer, StartEnd> timeCost;
public UndergroundSystem() {
map = new HashMap<>();
timeCost = new HashMap<>();
}
public void checkIn(int id, String stationName, int t) {
Event event = new Event(id, stationName, t);
map.put(id, event);
}
public void checkOut(int id, String stationName, int t) {
Event event = map.get(id);
timeCost.put(id, new StartEnd(event.name, stationName, t - event.time));
}
public double getAverageTime(String startStation, String endStation) {
double count = 0;
double sum = 0;
double avage = 0.0;
for (Map.Entry<Integer, StartEnd> x : timeCost.entrySet()) {
StartEnd st = x.getValue();
if (st.start.equals(startStation) && st.end.equals(endStation)){
count++;
sum += st.cost;
}
}
avage = (double) sum / count;
return avage;
}
}
public class Main {
public static void main(String[] args) {
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(45, "Leyton", 3);
undergroundSystem.checkIn(32, "Paradise", 8);
undergroundSystem.checkIn(27, "Leyton", 10);
undergroundSystem.checkOut(45, "Waterloo", 15); // 乘客 45 "Leyton" -> "Waterloo" ,用时 15-3 = 12
undergroundSystem.checkOut(27, "Waterloo", 20); // 乘客 27 "Leyton" -> "Waterloo" ,用时 20-10 = 10
undergroundSystem.checkOut(32, "Cambridge", 22); // 乘客 32 "Paradise" -> "Cambridge" ,用时 22-8 = 14
undergroundSystem.getAverageTime("Paradise", "Cambridge"); // 返回 14.00000 。只有一个 "Paradise" -> "Cambridge" 的行程,(14) / 1 = 14
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // 返回 11.00000 。有两个 "Leyton" -> "Waterloo" 的行程,(10 + 12) / 2 = 11
undergroundSystem.checkIn(10, "Leyton", 24);
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // 返回 11.00000
undergroundSystem.checkOut(10, "Waterloo", 38); // 乘客 10 "Leyton" -> "Waterloo" ,用时 38-24 = 14
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // 返回 12.00000 。有三个 "Leyton" -> "Waterloo" 的行程,(10 + 12 + 14) / 3 = 12
}
}
写在后面
从22年12月前后开始刷leecode, 中间陆陆续续有中断、有痛苦、有放弃,到今天2023年8月19日又重新捡起, 太多的内心煎熬, 想放弃, 又想着能放弃吗? 就真的学不会吗? 就真的这么菜吗? 大佬不也是从新手开始的吗? 但不可否认的是, 这东西跟天赋有关系,跟个人的长年累月的积累也是有关系的。当然我也承认, 这东西说有用也有有用,说无用也不能否定。哎,想放弃,又想说我坚持一下再试试?算了吧,再试试