Skip to content

Commit 739e3f8

Browse files
committed
添加 0239.滑动窗口最大值.md C 版本
1 parent d66e733 commit 739e3f8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

problems/0239.滑动窗口最大值.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,38 @@ public:
890890
};
891891
```
892892
893+
### C
894+
895+
```c
896+
int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
897+
*returnSize = numsSize - k + 1;
898+
int *res = (int*)malloc((*returnSize) * sizeof(int));
899+
assert(res);
900+
int *deque = (int*)malloc(numsSize * sizeof(int));
901+
assert(deque);
902+
int front = 0, rear = 0, idx = 0;
903+
904+
for (int i = 0 ; i < numsSize ; i++) {
905+
while (front < rear && deque[front] <= i - k) {
906+
front++;
907+
}
908+
909+
while (front < rear && nums[deque[rear - 1]] <= nums[i]) {
910+
rear--;
911+
}
912+
913+
deque[rear++] = i;
914+
915+
if (i >= k - 1) {
916+
res[idx++] = nums[deque[front]];
917+
}
918+
}
919+
920+
return res;
921+
}
922+
923+
```
924+
893925
<p align="center">
894926
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
895927
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
 (0)