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

[pull] master from youngyangyang04:master #308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 14 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 20, 2023
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
7d92df3
Update 0309.最佳买卖股票时机含冷冻期.md about rust
fwqaaq Jun 30, 2023
fc2a54a
Merge pull request #2160 from fwqaaq/patch-41
youngyangyang04 Jul 20, 2023
c3ebc91
更新 双指针总结 排版格式修复
jinbudaily Jul 20, 2023
de770c6
更新 栈与队列理论基础 排版格式修复
jinbudaily Jul 20, 2023
4b5e198
更新 0232.用栈实现队列 排版格式修复
jinbudaily Jul 20, 2023
c18dbf0
更新 0225.用队列实现栈 排版格式修复
jinbudaily Jul 20, 2023
a1ef2d0
更新 020.有效的括号 排版格式修复
jinbudaily Jul 20, 2023
70888c2
更新 1047.删除字符串中的所有相邻重复项 排版格式修复
jinbudaily Jul 20, 2023
7f4d740
更新 0150.逆波兰表达式求值 排版格式修复
jinbudaily Jul 20, 2023
5a6bf6d
更新 0239.滑动窗口最大值 排版格式修复
jinbudaily Jul 20, 2023
0e4f91d
更新 0347.前k个高频元素 排版格式修复
jinbudaily Jul 20, 2023
f7b87e6
更新 栈与队列总结 排版格式修复
jinbudaily Jul 20, 2023
5d76e86
Merge branch 'master' of github.com:jinbudaily/leetcode-master
jinbudaily Jul 20, 2023
e6b208a
Merge pull request #2194 from jinbudaily/master
youngyangyang04 Jul 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
更新 0347.前k个高频元素 排版格式修复
  • Loading branch information
jinbudaily committed Jul 20, 2023
commit 0e4f91d81d68b53269f33aa994ecab626202243b
33 changes: 19 additions & 14 deletions problems/0347.前K个高频元素.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>




> 前K个大数问题,老生常谈,不得不谈

# 347.前 K 个高频元素
Expand All @@ -29,9 +27,11 @@
* 题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的。
* 你可以按任意顺序返回答案。

# 思路
## 算法公开课

《代码随想录》算法视频公开课:[优先级队列正式登场!大顶堆、小顶堆该怎么用?| LeetCode:347.前 K 个高频元素](https://www.bilibili.com/video/BV1Xg41167Lz),相信结合视频在看本篇题解,更有助于大家对本题的理解。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[优先级队列正式登场!大顶堆、小顶堆该怎么用?| LeetCode:347.前 K 个高频元素](https://www.bilibili.com/video/BV1Xg41167Lz),相信结合视频再看本篇题解,更有助于大家对本题的理解**。

## 思路

这道题目主要涉及到如下三块内容:
1. 要统计元素出现频率
Expand Down Expand Up @@ -122,18 +122,18 @@ public:
* 时间复杂度: O(nlogk)
* 空间复杂度: O(n)

# 拓展
## 拓展
大家对这个比较运算在建堆时是如何应用的,为什么左大于右就会建立小顶堆,反而建立大顶堆比较困惑。

确实 例如我们在写快排的cmp函数的时候,`return left>right` 就是从大到小,`return left<right` 就是从小到大。

优先级队列的定义正好反过来了,可能和优先级队列的源码实现有关(我没有仔细研究),我估计是底层实现上优先队列队首指向后面,队尾指向最前面的缘故!


# 其他语言版本
## 其他语言版本

### Java:

Java:
```java

/*Comparator接口说明:
Expand Down Expand Up @@ -216,7 +216,8 @@ class Solution {
}
```

Python:
### Python:

```python
#时间复杂度:O(nlogk)
#空间复杂度:O(n)
Expand Down Expand Up @@ -245,7 +246,7 @@ class Solution:
return result
```

Go:
### Go:

```go
//方法一:小顶堆
Expand Down Expand Up @@ -320,8 +321,8 @@ func topKFrequent(nums []int, k int) []int {



### JavaScript:

JavaScript:
```js
// js 没有堆 需要自己构造
class Heap {
Expand Down Expand Up @@ -419,7 +420,7 @@ const topKFrequent = function (nums, k) {
};
```

TypeScript:
### TypeScript:

```typescript
function topKFrequent(nums: number[], k: number): number[] {
Expand All @@ -435,7 +436,8 @@ function topKFrequent(nums: number[], k: number): number[] {
};
```

C#:
### C#:

```csharp
public int[] TopKFrequent(int[] nums, int k) {
//哈希表-标权重
Expand Down Expand Up @@ -473,7 +475,7 @@ C#:

```

Scala:
### Scala:

解法一: 优先级队列
```scala
Expand Down Expand Up @@ -517,7 +519,9 @@ object Solution {
}
```

rust: 小根堆
### Rust

小根堆

```rust
use std::cmp::Reverse;
Expand Down Expand Up @@ -549,3 +553,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

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