class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * return topK string * @param strings string字符串vector strings * @param k int整型 the k * @return string字符串vector<vector<>> */ struct Node { string key; int cnt; Node(string k, int c): key(k), cnt(c) {} bool operator<(const Node& r) const { if (cnt != r.cnt) { return cnt > r.cnt; } return key < r.key; } }; vector<vector<string> > topKstrings(vector<string>& strings, int kk) { unordered_map<string, int> hash; priority_queue<Node> q; for (auto& s : strings) hash[s]++; for (auto &[k, v] : hash) { Node node(k, v); q.push(node); if(q.size() > kk) q.pop(); } vector<vector<string> > ans; while(q.size()){ ans.push_back({q.top().key,to_string(q.top().cnt)}); q.pop(); } reverse(ans.begin(),ans.end()); return ans; } };