上次编辑到这里,代码来自缓存 点击恢复默认模板
class UndergroundSystem {
public:
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
}
void checkOut(int id, string stationName, int t) {
}
double getAverageTime(string startStation, string endStation) {
}
};
/**
* 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);
*/
golang 解法, 执行用时: 120 ms, 内存消耗: 15.6 MB, 提交时间: 2023-06-07 11:17:06
type UndergroundSystem struct {
data map[string]map[string]*Stat
checkInData map[int]*CheckIn
}
type CheckIn struct{
id int
stationName string
t int
}
type Stat struct{
cnt int
total int
}
func Constructor() UndergroundSystem {
return UndergroundSystem{
data: make(map[string]map[string]*Stat, 0),
checkInData: make(map[int]*CheckIn, 0),
}
}
func (this *UndergroundSystem) CheckIn(id int, stationName string, t int) {
this.checkInData[id] = &CheckIn{
id: id,
stationName: stationName,
t: t,
}
}
func (this *UndergroundSystem) CheckOut(id int, stationName string, t int) {
checkIn := this.checkInData[id]
if checkIn != nil {
if _, ok := this.data[checkIn.stationName]; !ok {
this.data[checkIn.stationName] = make(map[string]*Stat, 0)
}
if _, ok := this.data[checkIn.stationName][stationName]; !ok {
this.data[checkIn.stationName][stationName] = &Stat{
cnt: 1,
total: t-checkIn.t,
}
return
}
this.data[checkIn.stationName][stationName].cnt++
this.data[checkIn.stationName][stationName].total+=t-checkIn.t
}
}
func (this *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
if stat, ok := this.data[startStation][endStation]; ok {
return float64(stat.total) / float64(stat.cnt)
}
return float64(0)
}
/**
* Your UndergroundSystem object will be instantiated and called as such:
* obj := Constructor();
* obj.CheckIn(id,stationName,t);
* obj.CheckOut(id,stationName,t);
* param_3 := obj.GetAverageTime(startStation,endStation);
*/
/**
* Your UndergroundSystem object will be instantiated and called as such:
* obj := Constructor();
* obj.CheckIn(id,stationName,t);
* obj.CheckOut(id,stationName,t);
* param_3 := obj.GetAverageTime(startStation,endStation);
*/
golang 解法, 执行用时: 120 ms, 内存消耗: 15.9 MB, 提交时间: 2023-06-07 11:15:33
type user struct {
start, end string
t1, t2 int
}
type UndergroundSystem struct {
users map[int]*user
sum map[string][]int
}
func Constructor() UndergroundSystem {
return UndergroundSystem{make(map[int]*user), make(map[string][]int)}
}
func (this *UndergroundSystem) CheckIn(id int, stationName string, t int) {
this.users[id] = &user{
start: stationName,
t1: t,
}
}
func (this *UndergroundSystem) CheckOut(id int, stationName string, t int) {
key := this.users[id].start + "=>" + stationName
if _, ok := this.sum[key]; !ok {
this.sum[key] = []int{0, 0}
}
this.sum[key][0] += t - this.users[id].t1
this.sum[key][1] ++
}
func (this *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
key := startStation + "=>" + endStation
return float64(this.sum[key][0])/float64(this.sum[key][1])
}
/**
* Your UndergroundSystem object will be instantiated and called as such:
* obj := Constructor();
* obj.CheckIn(id,stationName,t);
* obj.CheckOut(id,stationName,t);
* param_3 := obj.GetAverageTime(startStation,endStation);
*/
python3 解法, 执行用时: 168 ms, 内存消耗: 28.7 MB, 提交时间: 2023-06-07 11:14:27
class UndergroundSystem:
def __init__(self):
self.startInfo = dict()
self.table = dict()
def checkIn(self, id: int, stationName: str, t: int) -> None:
self.startInfo[id] = (stationName, t)
def checkOut(self, id: int, stationName: str, t: int) -> None:
startTime = self.startInfo[id][1]
index = (self.startInfo[id][0], stationName)
rec = self.table.get(index, (0, 0))
self.table[index] = (rec[0] + t - startTime, rec[1] + 1)
def getAverageTime(self, startStation: str, endStation: str) -> float:
index = (startStation, endStation)
sum, amount = self.table[index]
return sum / amount
# Your UndergroundSystem object will be instantiated and called as such:
# obj = UndergroundSystem()
# obj.checkIn(id,stationName,t)
# obj.checkOut(id,stationName,t)
# param_3 = obj.getAverageTime(startStation,endStation)
java 解法, 执行用时: 106 ms, 内存消耗: 53.3 MB, 提交时间: 2023-06-07 11:13:38
class UndergroundSystem {
class Start {
String station;
int time;
public Start(String station, int time) {
this.station = station;
this.time = time;
}
}
class StartEnd {
String start;
String end;
public StartEnd(String start, String end) {
this.start = start;
this.end = end;
}
public int hashCode() {
return (start + end).hashCode();
}
public boolean equals(Object obj2) {
if (obj2 instanceof StartEnd) {
StartEnd startEnd2 = (StartEnd) obj2;
return this.start.equals(startEnd2.start) && this.end.equals(startEnd2.end);
}
return false;
}
}
class SumAmount {
int sum;
int amount;
public SumAmount(int sum, int amount) {
this.sum = sum;
this.amount = amount;
}
}
Map<Integer, Start> startInfo;
Map<StartEnd, SumAmount> table;
public UndergroundSystem() {
startInfo = new HashMap<Integer, Start>();
table = new HashMap<StartEnd, SumAmount>();
}
public void checkIn(int id, String stationName, int t) {
startInfo.put(id, new Start(stationName, t));
}
public void checkOut(int id, String stationName, int t) {
Start start = startInfo.get(id);
String startStation = start.station;
int startTime = start.time;
StartEnd startEnd = new StartEnd(startStation, stationName);
SumAmount sumAmount = table.getOrDefault(startEnd, new SumAmount(0, 0));
sumAmount.sum += t - startTime;
sumAmount.amount++;
table.put(startEnd, sumAmount);
}
public double getAverageTime(String startStation, String endStation) {
StartEnd index = new StartEnd(startStation, endStation);
SumAmount sumAmount = table.get(index);
int sum = sumAmount.sum, amount = sumAmount.amount;
return 1.0 * sum / amount;
}
}
/**
* 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);
*/
cpp 解法, 执行用时: 136 ms, 内存消耗: 59.4 MB, 提交时间: 2023-06-07 11:13:23
class UndergroundSystem {
public:
using Start = pair <string, int>;
using StartEnd = pair <string, string>;
using SumAndAmount = pair <int, int>;
struct StartEndHash {
int operator() (const StartEnd& x) const{
return hash <string> () (x.first + x.second);
}
};
unordered_map <int, Start> startInfo;
unordered_map <StartEnd, SumAndAmount, StartEndHash> table;
UndergroundSystem() {}
void checkIn(int id, string stationName, int t) {
startInfo[id] = {stationName, t};
}
void checkOut(int id, string stationName, int t) {
auto startTime = startInfo[id].second;
table[{startInfo[id].first, stationName}].first += t - startTime;
table[{startInfo[id].first, stationName}].second++;
}
double getAverageTime(string startStation, string endStation) {
auto sum = table[{startStation, endStation}].first;
auto amount = table[{startStation, endStation}].second;
return double(sum) / amount;
}
};
/**
* 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);
*/