Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 230e9d9

Browse files
Merge pull request youngyangyang04#1600 from Jamcy123/347.前K个高频元素
347.前K个高频元素 JavaScript版本修改 原版JS代码无法提交报错
2 parents 2669ec0 + 300922d commit 230e9d9

File tree

1 file changed

+80
-77
lines changed

1 file changed

+80
-77
lines changed

‎problems/0347.前K个高频元素.md‎

Lines changed: 80 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -268,99 +268,102 @@ func topKFrequent(nums []int, k int) []int {
268268

269269

270270

271-
javaScript:
271+
JavaScript:
272272
```js
273-
/**
274-
* @param{number[]}nums
275-
* @param{number}k
276-
* @return{number[]}
277-
*/
278-
vartopKFrequent=function(nums, k) {
279-
constmap=newMap();
280-
281-
for(constnumof nums) {
282-
map.set(num, (map.get(num) ||0) +1);
283-
}
273+
// js 没有堆 需要自己构造
274+
classHeap {
275+
constructor(compareFn) {
276+
this.compareFn= compareFn;
277+
this.queue= [];
278+
}
279+
280+
// 添加
281+
push(item) {
282+
// 推入元素
283+
this.queue.push(item);
284284

285-
// 创建小顶堆
286-
const priorityQueue = new PriorityQueue((a, b) => a[1] - b[1]);
285+
// 上浮
286+
let index = this.size() - 1; // 记录推入元素下标
287+
let parent = Math.floor((index - 1) / 2); // 记录父节点下标
287288

288-
// entry 是一个长度为2的数组,0位置存储key,1位置存储value
289-
for (const entry of map.entries()) {
290-
priorityQueue.push(entry);
291-
if (priorityQueue.size() > k) {
292-
priorityQueue.pop();
289+
while (parent >= 0 && this.compare(parent, index) > 0) { // 注意compare参数顺序
290+
[this.queue[index], this.queue[parent]] = [this.queue[parent], this.queue[index]];
291+
292+
// 更新下标
293+
index = parent;
294+
parent = Math.floor((index - 1) / 2);
295+
}
293296
}
294-
}
295297

296-
const ret = [];
298+
// 获取堆顶元素并移除
299+
pop() {
300+
// 堆顶元素
301+
const out = this.queue[0];
297302

298-
for(let i = priorityQueue.size() - 1; i >= 0; i--) {
299-
ret[i] = priorityQueue.pop()[0];
300-
}
303+
// 移除堆顶元素 填入最后一个元素
304+
this.queue[0] = this.queue.pop();
301305

302-
return ret;
303-
};
306+
// 下沉
307+
let index = 0; // 记录下沉元素下标
308+
let left = 1; // left 是左子节点下标 left + 1 则是右子节点下标
309+
let searchChild = this.compare(left, left + 1) > 0 ? left + 1 : left;
304310

311+
while (searchChild !== undefined && this.compare(index, searchChild) > 0) { // 注意compare参数顺序
312+
[this.queue[index], this.queue[searchChild]] = [this.queue[searchChild], this.queue[index]];
305313

306-
function PriorityQueue(compareFn) {
307-
this.compareFn = compareFn;
308-
this.queue = [];
309-
}
314+
// 更新下标
315+
index = searchChild;
316+
left = 2 * index + 1;
317+
searchChild = this.compare(left, left + 1) > 0 ? left + 1 : left;
318+
}
310319

311-
// 添加
312-
PriorityQueue.prototype.push = function(item) {
313-
this.queue.push(item);
314-
let index = this.queue.length - 1;
315-
let parent = Math.floor((index - 1) / 2);
316-
// 上浮
317-
while(parent >= 0 && this.compare(parent, index) > 0) {
318-
// 交换
319-
[this.queue[index], this.queue[parent]] = [this.queue[parent], this.queue[index]];
320-
index = parent;
321-
parent = Math.floor((index - 1) / 2);
322-
}
323-
}
320+
return out;
321+
}
324322

325-
// 获取堆顶元素并移除
326-
PriorityQueue.prototype.pop = function() {
327-
const ret = this.queue[0];
328-
329-
// 把最后一个节点移到堆顶
330-
this.queue[0] = this.queue.pop();
331-
332-
let index = 0;
333-
// 左子节点下标,left + 1 就是右子节点下标
334-
let left = 1;
335-
let selectedChild = this.compare(left, left + 1) > 0 ? left + 1 : left;
336-
337-
// 下沉
338-
while(selectedChild !== undefined && this.compare(index, selectedChild) > 0) {
339-
// 交换
340-
[this.queue[index], this.queue[selectedChild]] = [this.queue[selectedChild], this.queue[index]];
341-
index = selectedChild;
342-
left = 2 * index + 1;
343-
selectedChild = this.compare(left, left + 1) > 0 ? left + 1 : left;
344-
}
323+
size() {
324+
return this.queue.length;
325+
}
345326

346-
return ret;
347-
}
327+
// 使用传入的 compareFn 比较两个位置的元素
328+
compare(index1, index2) {
329+
// 处理下标越界问题
330+
if (this.queue[index1] === undefined) return 1;
331+
if (this.queue[index2] === undefined) return -1;
332+
333+
return this.compareFn(this.queue[index1], this.queue[index2]);
334+
}
348335

349-
PriorityQueue.prototype.size = function() {
350-
return this.queue.length;
351336
}
352337

353-
// 使用传入的 compareFn 比较两个位置的元素
354-
PriorityQueue.prototype.compare = function(index1, index2) {
355-
if (this.queue[index1] === undefined) {
356-
return 1;
357-
}
358-
if (this.queue[index2] === undefined) {
359-
return -1;
360-
}
338+
const topKFrequent = function (nums, k) {
339+
const map = new Map();
361340

362-
return this.compareFn(this.queue[index1], this.queue[index2]);
363-
}
341+
for (const num of nums) {
342+
map.set(num, (map.get(num) || 0) + 1);
343+
}
344+
345+
// 创建小顶堆
346+
const heap= new Heap((a, b) => a[1] - b[1]);
347+
348+
// entry 是一个长度为2的数组,0位置存储key,1位置存储value
349+
for (const entry of map.entries()) {
350+
heap.push(entry);
351+
352+
if (heap.size() > k) {
353+
heap.pop();
354+
}
355+
}
356+
357+
// return heap.queue.map(e => e[0]);
358+
359+
const res = [];
360+
361+
for (let i = heap.size() - 1; i >= 0; i--) {
362+
res[i] = heap.pop()[0];
363+
}
364+
365+
return res;
366+
};
364367
```
365368

366369
TypeScript:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /