class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
map<string, int> m;
for(int i = 0; i < words.size(); ++i){
++m[words[i]];
}
multimap<int,string,greater<int>> mp;
for(auto e: m){
mp.insert(make_pair(e.second, e.first));
}
vector<string> ret;
for(auto e : mp){
ret.push_back(e.second);
if(--k == 0){
break;
}
}
return ret;
}
};
前K个高频单词
最新推荐文章于 2023-05-23 21:54:02 发布