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 c78f6ad

Browse files
Merge pull request MisterBooo#57 from zjming/master
0155-0215-0290-0394-0946
2 parents 846a690 + b6bdf8c commit c78f6ad

File tree

21 files changed

+548
-0
lines changed

21 files changed

+548
-0
lines changed

‎0155-min-stack/Animation/1.mp4‎

1.27 MB
Binary file not shown.
8.75 MB
Loading[フレーム]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#### 题目描述
2+
3+
> 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
4+
>
5+
> + push(x) —— 将元素 x 推入栈中。
6+
> + pop() —— 删除栈顶的元素。
7+
> + top() —— 获取栈顶元素。
8+
> + getMin() —— 检索栈中的最小元素。
9+
10+
```java
11+
示例:
12+
MinStack minStack = new MinStack();
13+
minStack.push(-2);
14+
minStack.push(0);
15+
minStack.push(-5);
16+
minStack.push(1)
17+
minStack.getMin(); --> 返回 -5.
18+
minStack.pop();
19+
minStack.top(); --> 返回 -5.
20+
minStack.getMin(); --> 返回 -5.
21+
```
22+
23+
#### 题目解析
24+
25+
为了能在常数时间内检测到栈中的最小元素,我们可以通过"空间换时间"的方式进行实现,为栈本身(数据栈\_data)增加一个辅助栈(最小值栈\_min)。每一次元素入 \_data 栈,则在 \_min 栈中增加对应的最小值;当 \_data 栈中的元素出栈,则 \_min 栈也进行出栈操作
26+
27+
#### 动画理解
28+
29+
![](../Animation/Animation.gif)
30+
31+
#### 代码实现
32+
33+
```java
34+
class MinStack {
35+
36+
private Stack<Integer> _data;
37+
private Stack<Integer> _min;
38+
39+
/** initialize your data structure here. */
40+
public MinStack() {
41+
_data = new Stack<>();
42+
_min = new Stack<>();
43+
}
44+
45+
public void push(int x) {
46+
_data.add(x);
47+
if (_min.isEmpty()){
48+
_min.push(x);
49+
}
50+
else{
51+
if (x > _min.peek()){
52+
x = _min.peek();
53+
}
54+
_min.push(x);
55+
}
56+
}
57+
58+
public void pop() {
59+
_data.pop();
60+
_min.pop();
61+
}
62+
63+
public int top() {
64+
return _data.peek();
65+
}
66+
67+
public int getMin() {
68+
return _min.peek();
69+
}
70+
}
71+
```
72+
73+
#### 复杂度分析
74+
75+
+ 时间复杂度:O(1)。
76+
+ 空间复杂度:O(n)。
77+

‎0155-min-stack/Code/1.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class MinStack {
2+
3+
private Stack<Integer> _data;
4+
private Stack<Integer> _min;
5+
6+
/** initialize your data structure here. */
7+
public MinStack() {
8+
_data = new Stack<>();
9+
_min = new Stack<>();
10+
}
11+
12+
public void push(int x) {
13+
_data.add(x);
14+
if (_min.isEmpty()){
15+
_min.push(x);
16+
}
17+
else{
18+
if (x > _min.peek()){
19+
x = _min.peek();
20+
}
21+
_min.push(x);
22+
}
23+
}
24+
25+
public void pop() {
26+
_data.pop();
27+
_min.pop();
28+
}
29+
30+
public int top() {
31+
return _data.peek();
32+
}
33+
34+
public int getMin() {
35+
return _min.peek();
36+
}
37+
}
1.81 MB
Binary file not shown.
7.78 MB
Loading[フレーム]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# LeetCode 第 215 号问题:数组中的第K个最大元素
2+
3+
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
4+
>
5+
> 同步博客:https://www.algomooc.com
6+
7+
题目来源于 LeetCode 上第 215 号问题:数组中的第K个最大元素。题目难度为 Medium,目前通过率为 62.0% 。
8+
9+
#### 题目描述
10+
11+
> 在未排序的数组中找到第 **k** 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
12+
>
13+
14+
```java
15+
示例1:
16+
输入: [3,2,1,5,6,4] 和 k = 2
17+
输出: 5
18+
示例2:
19+
输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
20+
输出: 4
21+
```
22+
23+
#### 题目解析
24+
25+
维护一个K大小的最小堆,堆中元素个数小于K时,新元素直接进入堆中;否则,当堆顶小于新元素时,弹出堆顶,将新元素加入堆。
26+
27+
由于堆是最小堆,堆顶是堆中的最小元素,新元素都会保证比堆顶小(否则新元素替换堆顶),故堆中K个元素是已扫描元素里最大的K个;堆顶元素即为第K大数。
28+
29+
#### 动画理解
30+
31+
![](../Animation/Animation.gif)
32+
33+
#### 代码实现
34+
35+
Java语言
36+
37+
```java
38+
class Solution {
39+
public int findKthLargest(int[] nums, int k) {
40+
// // 创建一个小顶堆(优先队列模拟)
41+
PriorityQueue<Integer> heap =
42+
new PriorityQueue<Integer>();
43+
44+
// 在堆中维护当前最大k个元素
45+
for (int i = 0; i < nums.length; i++){
46+
if(heap.size() < k){
47+
heap.add(nums[i]);
48+
}else if (heap.element() < nums[i]){
49+
heap.poll();
50+
heap.add(nums[i]);
51+
}
52+
}
53+
return heap.poll();
54+
}
55+
}
56+
```
57+
58+
C++语言实现
59+
60+
```c++
61+
#include <queue>
62+
63+
class Solution {
64+
public:
65+
int findKthLargest(vector<int>& nums, int k) {
66+
priority_queue<int, vector<int>, greater<int> > Q;
67+
for(int i = 0; i < nums.size(); i++){
68+
if(Q.size() < k){
69+
Q.push(nums[i]);
70+
}
71+
else if(Q.top() < nums[i]){
72+
Q.pop();
73+
Q.push(nums[i]);
74+
}
75+
}
76+
77+
return Q.top();
78+
}
79+
};
80+
```
81+
82+
#### 复杂度分析
83+
84+
+ 时间复杂度:向大小为 k 的堆中添加元素的时间复杂度为O(logK),重复该操作 N 次,故总时间复杂度为 O(NlogK)
85+
+ 空间复杂度:O(K)
86+
87+
88+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <queue>
2+
3+
class Solution {
4+
public:
5+
int findKthLargest(vector<int>& nums, int k) {
6+
priority_queue<int, vector<int>, greater<int> > Q;
7+
for(int i = 0; i < nums.size(); i++){
8+
if(Q.size() < k){
9+
Q.push(nums[i]);
10+
}
11+
else if(Q.top() < nums[i]){
12+
Q.pop();
13+
Q.push(nums[i]);
14+
}
15+
}
16+
17+
return Q.top();
18+
}
19+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int findKthLargest(int[] nums, int k) {
3+
// // 创建一个小顶堆(优先队列模拟)
4+
PriorityQueue<Integer> heap =
5+
new PriorityQueue<Integer>();
6+
7+
// 在堆中维护当前最大k个元素
8+
for (int i = 0; i < nums.length; i++){
9+
if(heap.size() < k){
10+
heap.add(nums[i]);
11+
}else if (heap.element() < nums[i]){
12+
heap.poll();
13+
heap.add(nums[i]);
14+
}
15+
}
16+
return heap.poll();
17+
}
18+
}

‎0290-Word-Pattern/Animation/1.mp4‎

10.9 MB
Binary file not shown.

0 commit comments

Comments
(0)

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