LeetCode 692
自定义结构体也可以在结构体内重载 <
class Solution {
public:
typedef pair<int, string> PIS;
struct comp {
bool operator() (PIS& a, PIS& b) {
if (a.first != b.first) return a.first < b.first;
return a.second > b.second;
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
map<string, int> hash;
for (auto word : words) hash[word] ++;
auto cmp = [](const PIS& a, const PIS& b){
if (a.first != b.first) return a.first < b.first;
return a.second > b.second;
};
//priority_queue<PIS, vector<PIS>, comp> heap;
priority_queue<PIS, vector<PIS>, decltype(cmp)> heap(cmp);
for (auto iter : hash) heap.push({iter.second, iter.first});
vector<string> res;
while(k --) res.push_back(heap.top().second), heap.pop();
return res;
}
};
兄弟有时间填个邀请码hhhhhhhhh(可以得AC币,邀请码在学生认证那填) 我的邀请码是:GUDFH
之前填过了~~