File tree Expand file tree Collapse file tree 1 file changed +13
-11
lines changed Expand file tree Collapse file tree 1 file changed +13
-11
lines changed Original file line number Diff line number Diff line change @@ -64210,28 +64210,30 @@ var topKFrequent = function(words, k) {
64210
64210
wordToFreq.set(word, wordToFreq.get(word) + 1 || 1);
64211
64211
}
64212
64212
64213
- let pq = new PriorityQueue((a, b) => {
64214
- if (a.freq === b.freq) {
64215
- // 如果出现频率相同,按照字符串字典序排序
64216
- return b.word.localeCompare(a.word);
64217
- }
64218
- // 队列按照字符串出现频率从小到大排序
64219
- return a.freq - b.freq;
64213
+ let pq = new PriorityQueue({
64214
+ compare: (a, b) => {
64215
+ if (a.freq === b.freq) {
64216
+ // 如果出现频率相同,按照字符串字典序排序
64217
+ return b.word.localeCompare(a.word);
64218
+ }
64219
+ // 队列按照字符串出现频率从小到大排序
64220
+ return a.freq - b.freq;
64221
+ },
64220
64222
});
64221
64223
64222
64224
// 按照字符串频率升序排序
64223
64225
for (let [word, freq] of wordToFreq.entries()) {
64224
- pq.offer ({ word, freq });
64225
- if (pq.size > k) {
64226
+ pq.enqueue ({ word, freq });
64227
+ if (pq.size() > k) {
64226
64228
// 维护出现频率最多的 k 个单词
64227
- pq.poll ();
64229
+ pq.dequeue ();
64228
64230
}
64229
64231
}
64230
64232
64231
64233
// 把出现次数最多的 k 个字符串返回
64232
64234
let res = [];
64233
64235
while (!pq.isEmpty()) {
64234
- res.push(pq.poll ().word);
64236
+ res.push(pq.dequeue ().word);
64235
64237
}
64236
64238
return res.reverse();
64237
64239
};
You can’t perform that action at this time.
0 commit comments