Skip to content

Commit 4d550a5

Browse files
Refine
Signed-off-by: begeekmyfriend <[email protected]>
1 parent 308683c commit 4d550a5

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

021_merge_two_sorted_lists/merge_lists.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
1212
struct ListNode *prev = &dummy;
1313
dummy.next = l1;
1414

15-
while (l1 != NULL || l2 != NULL) {
15+
while (l1 != NULL && l2 != NULL) {
1616
if (l1->val <= l2->val) {
1717
prev = l1;
1818
l1 = l1->next;

239_sliding_window_maximum/slide_window.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize)
1313
int *results = malloc((numsSize - k + 1) * sizeof(int));
1414

1515
for (i = 0; i < numsSize; i++) {
16-
/* monotonous decreasing */
16+
/* keep the elements in slide window monotonous decreasing */
1717
while (tail > head && nums[i] >= nums[indexes[tail - 1]]) {
18+
/* squeeze out the previous smaller ones */
1819
tail--;
1920
}
20-
indexes[tail++] = i;
2121

22+
/* Pipe: first in last out */
23+
indexes[tail++] = i;
2224
if (indexes[head] <= i - k) {
2325
head++;
2426
}
2527

28+
/* k - 1 is the end of the first sliding window */
2629
if (i >= k - 1) {
2730
results[count++] = nums[indexes[head]];
2831
}

0 commit comments

Comments
 (0)