class Solution {
public:
/**
* return topK string
* @param strings string字符串vector strings
* @param k int整型 the k
* @return string字符串vector<vector<>>
*/
/*
map<string, int>
priorty<int, string>
vector<>
*/
static bool cmp(vector<string> &a, vector<string> &b) {
if (a[1] == b[1]) {
return a[0] < b[0];
}
return stoi(a[1]) > stoi(b[1]);
}
vector<vector<string> > topKstrings(vector<string>& strings, int k) {
// write code here
int len = strings.size();
map<string, int>mp;
// 1, 将数据记录到map中
for (int i = 0; i < len; i++) {
mp[strings[i]]++;
}
map<string, int>::iterator it = mp.begin();
vector<vector<string>> ret;
// 2, 将数据记录到vector中
for (; it != mp.end(); it++) {
ret.push_back({it->first, to_string(it->second)});
}
// 3,比较
sort(ret.begin(), ret.end(), cmp);
ret.resize(k);
return ret;
}
};