static function
作者:
离我代码远点
,
2021-03-25 12:18:07
,
所有人可见
,
阅读 302
class Solution {
public:
static bool cmp(const pair<string,int> &x , const pair<string,int> &y)
{
return (x.second == y.second) ? x.first < y.first : x.second > y.second;
}
vector<string> topKFrequent(vector<string>& words, int k) {
vector<string> res;
map<string,int> Mymap;
for(auto word : words) Mymap[word]++;
vector<pair<string,int>> v(Mymap.begin(), Mymap.end());
sort(v.begin(), v.end(), cmp);
int cnt = 0;
for(auto iter = v.begin(); iter != v.end(); ++iter)
{
res.push_back(iter->first);
cnt++;
if(cnt==k) break;
}
return res;
}
};