Skip to content

Commit c9882f6

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 0b01a3d commit c9882f6

File tree

5 files changed

+56
-50
lines changed

5 files changed

+56
-50
lines changed

044_wildcard_matching/wildcard_matching.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,40 @@
33
#include <stdbool.h>
44
#include <string.h>
55

6+
67
static bool isMatch(char* s, char* p)
78
{
89
char *last_s = NULL;
910
char *last_p = NULL;
10-
while(*s != '\0') {
11-
if (*p=='*'){
12-
//skip the "*", and mark a flag
13-
p++;
14-
//edge case
15-
if (*p=='\0') return true;
16-
//use last_s and last_p to store where the "*" match starts.
11+
while (*s != '\0') {
12+
if (*p == '*') {
13+
/* skip the '*', and mark a flag */
14+
if (*++p == '\0') {
15+
return true;
16+
}
17+
/* use last_s and last_p to store where the "*" match starts. */
1718
last_s = s;
1819
last_p = p;
19-
} else if (*p=='?' || *s == *p) {
20+
} else if (*p == '?' || *s == *p) {
2021
s++;
2122
p++;
2223
} else if (last_s != NULL) {
23-
// check "last_s" to know whether meet "*" before
24-
// if meet "*" previously, and the *s != *p
25-
// reset the p, using '*' to match this situation
24+
/* check "last_s" to know whether meet "*" before
25+
* if meet "*" previously, and the *s != *p
26+
* reset the p, using '*' to match this situation
27+
*/
2628
p = last_p;
2729
s = ++last_s;
2830
} else {
29-
// *p is not wildcard char,
30-
// doesn't match *s,
31-
// there are no '*' wildcard matched before
31+
/* *p is not wildcard char,
32+
* doesn't match *s,
33+
* there are no '*' wildcard matched before
34+
*/
3235
return false;
3336
}
3437
}
35-
//edge case: "s" is done, but "p" still have chars.
38+
39+
/* s is done, but "p" still have chars. */
3640
while (*p == '*') {
3741
p++;
3842
}

045_jump_game_ii/jump_game.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ 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 */
1615
for (i = lo; i <= hi; i++) {
1716
/* Assume right > hi for the purpose of the problem */
1817
right = max(i + nums[i], right);
1918
}
19+
/* [lo, hi] is the next location range */
2020
lo = hi + 1;
2121
hi = right;
2222
steps++;

048_rotate_image/rotate.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,45 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4-
static void rotate(int** matrix, int matrixRowSize, int matrixColSize)
4+
static void rotate(int** matrix, int matrixSize, int *matrixColSize)
55
{
66
int i, j;
7-
if (matrixRowSize != matrixColSize) {
8-
return;
9-
}
10-
11-
for (i = 0; i < matrixRowSize / 2; i++) {
12-
int low = i, high = matrixColSize - i - 1;
7+
for (i = 0; i < matrixSize / 2; i++) {
8+
int col_size = matrixColSize[i];
9+
int low = i, high = col_size - i - 1;
1310
for (j = low; j < high; j++) {
1411
int tmp = matrix[i][j];
15-
matrix[i][j] = matrix[matrixColSize - 1 - j][i];
16-
matrix[matrixColSize - 1 - j][i] = matrix[matrixRowSize - 1 - i][matrixColSize - 1 - j];
17-
matrix[matrixRowSize - 1 - i][matrixColSize - 1 - j] = matrix[j][matrixRowSize - 1 - i];
18-
matrix[j][matrixRowSize - 1 - i] = tmp;
12+
matrix[i][j] = matrix[col_size - 1 - j][i];
13+
matrix[col_size - 1 - j][i] = matrix[matrixSize - 1 - i][col_size - 1 - j];
14+
matrix[matrixSize - 1 - i][col_size - 1 - j] = matrix[j][matrixSize - 1 - i];
15+
matrix[j][matrixSize - 1 - i] = tmp;
1916
}
2017
}
2118
}
2219

2320
int main(int argc, char **argv)
2421
{
25-
if (argc != 3) {
26-
fprintf(stderr, "Usage: ./test 3 3\n");
22+
if (argc != 2) {
23+
fprintf(stderr, "Usage: ./test 3\n");
2724
}
2825

2926
int i, j, count = 0;
3027
int row_size = atoi(argv[1]);
31-
int col_size = atoi(argv[2]);
28+
int *col_sizes = malloc(row_size * sizeof(int));
3229
int **matrix = malloc(row_size * sizeof(int *));
3330
for (i = 0; i < row_size; i++) {
34-
matrix[i] = malloc(col_size * sizeof(int));
35-
for (j = 0; j < col_size; j++) {
31+
col_sizes[i] = row_size;
32+
matrix[i] = malloc(col_sizes[i] * sizeof(int));
33+
for (j = 0; j < col_sizes[i]; j++) {
3634
matrix[i][j] = ++count;
3735
printf("%d ", matrix[i][j]);
3836
}
3937
printf("\n");
4038
}
4139

42-
rotate(matrix, row_size, col_size);
43-
for (i = 0; i < col_size; i++) {
44-
for (j = 0; j < row_size; j++) {
40+
rotate(matrix, row_size, col_sizes);
41+
for (i = 0; i < row_size; i++) {
42+
for (j = 0; j < col_sizes[i]; j++) {
4543
printf("%02d ", matrix[i][j]);
4644
}
4745
putchar('\n');

049_group_anagrams/anagrams.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5+
56
struct word_hash {
67
char *word;
78
int num;
@@ -25,10 +26,10 @@ static inline int BKDRHash(char *s, size_t size)
2526

2627
/**
2728
** Return an array of arrays of size *returnSize.
28-
** The sizes of the arrays are returned as *columnSizes array.
29-
** Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
29+
** The sizes of the arrays are returned as *returnColumnSizes array.
30+
** Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
3031
**/
31-
static char*** groupAnagrams(char** strs, int strsSize, int** columnSizes, int* returnSize)
32+
static char*** groupAnagrams(char** strs, int strsSize, int* returnSize, int** returnColumnSizes)
3233
{
3334
int i, j, count = 0;
3435
int hash_size = strsSize;
@@ -53,10 +54,10 @@ static char*** groupAnagrams(char** strs, int strsSize, int** columnSizes, int*
5354
int k = 0;
5455
struct hlist_node *p;
5556
char ***lists = malloc(count * sizeof(char **));
56-
*columnSizes = malloc(count * sizeof(int));
57+
*returnColumnSizes = malloc(count * sizeof(int));
5758
for (i = 0; i < hash_size; i++) {
5859
if (ht[i].num > 0) {
59-
(*columnSizes)[k] = ht[i].num;
60+
(*returnColumnSizes)[k] = ht[i].num;
6061
lists[k] = malloc(ht[i].num * sizeof(char *));
6162
for (j = 0; j < ht[i].num; j++) {
6263
int index = ht[i].indexes[j];

239_sliding_window_maximum/slide_window.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,30 @@
77
*/
88
int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize)
99
{
10-
int i, head = 0, tail = 0;
10+
int i, left = 0, right = 0;
1111
int count = 0;
1212
int *indexes = malloc(numsSize * sizeof(int));
1313
int *results = malloc((numsSize - k + 1) * sizeof(int));
1414

1515
for (i = 0; i < numsSize; i++) {
1616
/* keep the elements in slide window monotonous decreasing */
17-
while (tail > head && nums[i] >= nums[indexes[tail - 1]]) {
17+
while (right > left && nums[i] >= nums[indexes[right - 1]]) {
1818
/* squeeze out the previous smaller ones */
19-
tail--;
19+
right--;
2020
}
2121

22-
/* Pipe: first in last out */
23-
indexes[tail++] = i;
24-
if (indexes[head] <= i - k) {
25-
head++;
26-
}
22+
/* In order to measure the moving size of the sliding window, we
23+
* need to store the index instead of element into the window.
24+
*/
25+
indexes[right++] = i;
2726

28-
/* k - 1 is the end of the first sliding window */
27+
/* let k = 1 to verify the corner case */
2928
if (i >= k - 1) {
30-
results[count++] = nums[indexes[head]];
29+
results[count++] = nums[indexes[left]];
30+
}
31+
32+
if (i - indexes[left] + 1 >= k) {
33+
left++;
3134
}
3235
}
3336

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