mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0239.滑动窗口最大值.md C 版本
This commit is contained in:
@ -890,6 +890,38 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
|
||||
```c
|
||||
int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
|
||||
*returnSize = numsSize - k + 1;
|
||||
int *res = (int*)malloc((*returnSize) * sizeof(int));
|
||||
assert(res);
|
||||
int *deque = (int*)malloc(numsSize * sizeof(int));
|
||||
assert(deque);
|
||||
int front = 0, rear = 0, idx = 0;
|
||||
|
||||
for (int i = 0 ; i < numsSize ; i++) {
|
||||
while (front < rear && deque[front] <= i - k) {
|
||||
front++;
|
||||
}
|
||||
|
||||
while (front < rear && nums[deque[rear - 1]] <= nums[i]) {
|
||||
rear--;
|
||||
}
|
||||
|
||||
deque[rear++] = i;
|
||||
|
||||
if (i >= k - 1) {
|
||||
res[idx++] = nums[deque[front]];
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user