Leetcode 692. Top K Frequent Words | | Leetcode 692. Top K Frequent Words 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution1234567891011121314151617181920212223242526bool compare(pair<string, int>& a, pair<string, int>& b) { if(a.second == b.second) { return a.first < b.first; } return a.second > b.second;}class Solution {public: vector<string> topKFrequent(vector<string>& words, int k) { vector<string> result; unordered_map<string, int> stat; for(string word: words) { stat[word]++; } vector<pair<string, int>> values; for(auto val: stat) { values.push_back(val); } sort(values.begin(), values.end(), compare); for(int i = 0; i < k; i++) { result.push_back(values[i].first); } return result; }}; Reference https://leetcode.com/problems/top-k-frequent-words/description/ 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏