Skip to content

Commit 463ef54

Browse files
Annotation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent acdf017 commit 463ef54

File tree

24 files changed

+213
-176
lines changed

24 files changed

+213
-176
lines changed

041_first_missing_positive/missing_positive.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ static int firstMissingPositive(int* nums, int numsSize)
1616

1717
int i = 0;
1818
while (i < numsSize) {
19-
if (nums[i] != i + 1 && nums[i] > 0 && nums[i] <= numsSize && nums[i] != nums[nums[i] - 1]) {
19+
/* nums[i] should be i+1 and nums[nums[i] - 1] should be nums[i] */
20+
if (nums[i] != i + 1 && nums[i] > 0 && nums[i] <= numsSize && nums[nums[i] - 1] != nums[i]) {
21+
/* nums[nums[i] - 1] <- nums[i] */
2022
swap(nums + i, nums + nums[i] - 1);
2123
} else {
2224
i++;

045_jump_game_ii/jump_game.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ static int jump(int* nums, int numsSize)
1212
int steps = 0;
1313
while (hi < numsSize - 1) {
1414
int right = 0;
15+
/* [lo, hi] is the next location range, find the farest jump */
1516
for (i = lo; i <= hi; i++) {
17+
/* Assume right > hi for the purpose of the problem */
1618
right = max(i + nums[i], right);
1719
}
1820
lo = hi + 1;

055_jump_game/jump_game.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static bool canJump(int* nums, int numsSize)
1414
if (pos < i || pos >= numsSize - 1) {
1515
break;
1616
}
17+
/* if all positive number it always can arrive. */
1718
pos = max(i + nums[i], pos);
1819
}
1920

075_sort_colors/sort_colors.c

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,38 @@ static inline void swap(int *a, int *b)
99
*b = tmp;
1010
}
1111

12-
/*
13-
* RED = 0
14-
* WHITE = 1
15-
* BLUE = 2
16-
*/
1712
static void sortColors(int* nums, int numsSize)
1813
{
19-
int i = 0, j = numsSize - 1;
20-
while (i < j) {
21-
while (nums[i] == 0 && i < j) {
22-
i++;
14+
int i, j = 0;
15+
for (i = 0; i < numsSize; i++) {
16+
if (nums[i] == 0) {
17+
swap(nums + j, nums + i);
18+
j++;
2319
}
24-
while (nums[j] != 0 && j > i) {
25-
j--;
26-
}
27-
swap(nums + i, nums + j);
2820
}
29-
j = numsSize - 1;
30-
while (i < j) {
31-
while (nums[i] == 1 && i < j) {
32-
i++;
33-
}
34-
while (nums[j] != 1 && j > i) {
35-
j--;
21+
22+
for (i = j; i < numsSize; i++) {
23+
if (nums[i] == 1) {
24+
swap(nums + j, nums + i);
25+
j++;
3626
}
37-
swap(nums + i, nums + j);
3827
}
3928
}
4029

4130
int main(int argc, char **argv)
4231
{
43-
if (argc != 2) {
44-
fprintf(stderr, "Usage: ./test colors\n");
32+
if (argc < 2) {
33+
fprintf(stderr, "Usage: ./test 2 0 2 1 1 0\n");
4534
exit(-1);
4635
}
47-
int i, count = strlen(argv[1]);
36+
int i, count = argc - 1;
4837
int *nums = malloc(count * sizeof(int));
4938
for (i = 0; i < count; i++) {
50-
nums[i] = argv[1][i] - '0';
39+
nums[i] = atoi(argv[i + 1]);
5140
}
5241
sortColors(nums, count);
5342
for (i = 0; i < count; i++) {
54-
printf("%d", nums[i]);
43+
printf("%d ", nums[i]);
5544
}
5645
printf("\n");
5746
return 0;

080_remove_duplicates_from_sorted_array_ii/rm_dups.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ static int removeDuplicates(int* nums, int numsSize)
99

1010
int i;
1111
int len = 0;
12-
int count = 0;
12+
int count = 1;
1313
for (i = 1; i < numsSize; i++) {
14+
/* Find the start position to be replaced */
1415
if (nums[len] == nums[i]) {
1516
if (count < 2) {
1617
count++;
18+
/* Replace in each iteration */
1719
nums[++len] = nums[i];
1820
}
1921
} else {
22+
/* Here there are more than 2 duplicates */
2023
count = 1;
2124
nums[++len] = nums[i];
2225
}

085_maximal_rectangle/maximal_rectangle.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ static inline int max(int a, int b)
1111
static int area_calc(int *heights, int size)
1212
{
1313
int *indexes = malloc(size * sizeof(int));
14-
int *left = malloc(size * sizeof(int));
15-
int *right = malloc(size * sizeof(int));
14+
int *lhist = malloc(size * sizeof(int));
15+
int *rhist = malloc(size * sizeof(int));
1616

1717
int i, pos = 0;
1818
for (i = 0; i < size; i++) {
19-
/* monotonous increasing stack */
19+
/* squeeze to keep monotonous increasing histograms */
2020
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) {
2121
pos--;
2222
}
23-
left[i] = pos == 0 ? -1 : indexes[pos - 1];
23+
lhist[i] = pos == 0 ? -1 : indexes[pos - 1];
2424
indexes[pos++] = i;
2525
}
2626

2727
pos = 0;
2828
for (i = size - 1; i >= 0; i--) {
29-
/* monotonous increasing stack */
29+
/* squeeze to keep monotonous increasing histograms */
3030
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) {
3131
pos--;
3232
}
33-
right[i] = pos == 0 ? size : indexes[pos - 1];
33+
rhist[i] = pos == 0 ? size : indexes[pos - 1];
3434
indexes[pos++] = i;
3535
}
3636

3737
int max_area = 0;
3838
for (i = 0; i < size; i++) {
39-
int area = heights[i] * (right[i] - left[i] - 1);
39+
int area = heights[i] * (rhist[i] - lhist[i] - 1);
4040
max_area = max(area, max_area);
4141
}
4242

086_partition_list/partition_list.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,37 @@ struct ListNode {
66
struct ListNode *next;
77
};
88

9-
struct ListNode* partition(struct ListNode* head, int x) {
9+
struct ListNode* partition(struct ListNode* head, int x)
10+
{
1011
struct ListNode dummy;
11-
struct ListNode *p = NULL, *start = &dummy, *pivot;
12+
struct ListNode *prev1 = &dummy, *pivot;
13+
1214
dummy.next = head;
1315
for (pivot = head; pivot != NULL; pivot = pivot->next) {
1416
if (pivot->val >= x) {
15-
/* start->next == pivot */
1617
break;
1718
}
18-
start = pivot;
19+
prev1 = pivot;
1920
}
2021

21-
struct ListNode *prev;
22-
for (p = pivot; p != NULL; p = p->next) {
22+
struct ListNode *p = pivot->next;
23+
struct ListNode *prev2 = pivot;
24+
while (p != NULL) {
2325
if (p->val < x) {
24-
prev->next = p->next;
25-
p->next = start->next;
26-
start->next = p;
27-
start = p;
28-
p = prev;
26+
/* deletion */
27+
prev2->next = p->next;
28+
/* insertion */
29+
p->next = prev1->next;
30+
prev1->next = p;
31+
/* iteration */
32+
prev1 = p;
33+
p = prev2->next;
34+
} else {
35+
prev2 = p;
36+
p = p->next;
2937
}
30-
prev = p;
3138
}
39+
3240
return dummy.next;
3341
}
3442

087_scramble_string/scramble_string.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,21 @@
1212
* or
1313
* isScramble(s1[0..j], s2[n - j - 1, n]) && isScramble(s1[j+1..n], s2[0..n - j])
1414
*/
15-
#define N 128
16-
1715
static bool scramble(char *s1, int low1, int high1, char *s2, int low2, int high2)
1816
{
1917
if (high1 - low1 != high2 - low2) {
2018
return false;
2119
} else if (!memcmp(s1 + low1, s2 + low2, high1 - low1 + 1)) {
2220
return true;
2321
} else {
24-
int i, c1[N], c2[N];
25-
memset(c1, 0, N * sizeof(int));
26-
memset(c2, 0, N * sizeof(int));
22+
int i, c1[128] = { 0 }, c2[128] = { 0 };
2723
for (i = low1; i <= high1; i++) {
2824
c1[s1[i]]++;
2925
}
3026
for (i = low2; i <= high2; i++) {
3127
c2[s2[i]]++;
3228
}
33-
if (memcmp(c1, c2, N * sizeof(int))) {
29+
if (memcmp(c1, c2, 128 * sizeof(int))) {
3430
return false;
3531
} else {
3632
int len = high1 - low1 + 1;

094_binary_tree_inorder_traversal/bst_inorder_traversal.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4-
struct TreeNode {
5-
int val;
6-
struct TreeNode *left;
7-
struct TreeNode *right;
8-
};
4+
struct TreeNode {
5+
int val;
6+
struct TreeNode *left;
7+
struct TreeNode *right;
8+
};
99

1010
static void traverse(struct TreeNode *node, int *result, int *count)
1111
{
12-
if (node->left != NULL) {
13-
traverse(node->left, result, count);
12+
if (node == NULL) {
13+
return;
1414
}
15-
15+
16+
traverse(node->left, result, count);
1617
result[*count] = node->val;
1718
(*count)++;
18-
19-
if (node->right != NULL) {
20-
traverse(node->right, result, count);
21-
}
19+
traverse(node->right, result, count);
2220
}
2321

2422
/**

113_path_sum_ii/path_sum.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,17 @@ static void dfs(struct TreeNode *node, int sum, int *stack, int len, int **resul
1212
{
1313
if (node == NULL) {
1414
return;
15-
}
16-
17-
sum -= node->val;
18-
if (node->left == NULL && node->right == NULL && sum == 0) {
15+
} else if (node->left == NULL && node->right == NULL && sum == node->val) {
1916
results[*count] = malloc((len + 1) * sizeof(int));
2017
memcpy(results[*count], stack, len * sizeof(int));
2118
results[*count][len] = node->val;
2219
sizes[*count] = len + 1;
2320
(*count)++;
24-
return;
21+
} else {
22+
stack[len] = node->val;
23+
dfs(node->left, sum - node->val, stack, len + 1, results, sizes, count);
24+
dfs(node->right, sum - node->val, stack, len + 1, results, sizes, count);
2525
}
26-
stack[len] = node->val;
27-
dfs(node->left, sum, stack, len + 1, results, sizes, count);
28-
dfs(node->right, sum, stack, len + 1, results, sizes, count);
2926
}
3027

3128
static int **pathSum(struct TreeNode *root, int sum, int **columnSizes, int *returnSize)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy