先建立一个哈希表按照访问员工把所有访问时间分类,再对每一个员工进行遍历,若发现满足条件则将该员工录入答案。
将访问时间的字符串转化成int数据,并以分钟的形式储存,以便计算。
C++ 代码
class Solution {
public:
vector<string> findHighAccessEmployees(vector<vector<string>>& access_times) {
map<string,vector<int>>m;//建立哈希表,将访问时间按照员工姓名排好
int cnt=0;
for(int i=0;i<access_times.size();i++)
{
string n=access_times[i][0];
string t=access_times[i][1];
int p=stoi(t.substr(0,2))*60+stoi(t.substr(2));//将访问时间转换成分钟,方便计算。
//stoi(const string*),string to integer,字符串转化int数值。
m[n].push_back(p);
}
vector<string>res;
for(auto &[n,t]:m)
{
sort(t.begin(),t.end());//对时间进行排序
for(int i=2;i<t.size();i++)
{
if(t[i]-t[i-2]<60)//如果发现某位员工在一小时内访问三次就录入答案
{
res.push_back(n);
break;
}
}
}
return res;
}
};