320-用map解决员工打卡问题
作者:互联网
用map解决员工打卡问题
题目如下
解题思路如下
map,一个是姓名,一个是时间。
姓名按照字典序直接排好序了。
一个人对应多个打卡时间。
用函数substr把字符串的小时和分钟分解出来。
用函数把stoi把字符串转成int的数字。
解题代码如下
#include<iostream>
#include<thread>
#include<map>
#include<list>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> alertNames(vector<string>& keyName, vector<string>& keyTime)
{
vector<string> res;
if ((keyName.size() != keyTime.size() || keyName.size() < 3)) return res;
map<string, vector<int>> name_time;
int n = keyName.size();
for (int i = 0; i < n; ++i)
{
string name = keyName[i], time = keyTime[i];
string h = time.substr(0, 2), m = time.substr(3, 2);
int x = stoi(h) * 60 + stoi(m);
name_time[name].push_back(x);
}
for (auto& x : name_time)
{
if (x.second.size() >=3)
{
sort(x.second.begin(), x.second.end());
for (int i = 2; i < x.second.size(); ++i)
{
if (x.second[i - 2] + 60 >= x.second[i])
{
res.push_back(x.first);
break;
}
}
}
}
return res;
}
int main()
{
vector<string> keyname = { "leslie","leslie" ,"leslie" ,"clare","clare" ,"clare" ,"clare" };
vector<string> keytime = { "13:00", "13:20", "14:00", "18:00", "18:51", "19:30", "19:45" };
vector<string> res = alertNames(keyname, keytime);
for (auto& x : res)
{
cout << x << endl;
}
return 0;
}
运行截图如下
下面两种写法都可以
标签:map,int,res,second,320,vector,打卡,include,size 来源: https://blog.csdn.net/LINZEYU666/article/details/117198942