Skip to content

Commit 934b7b7

Browse files
Improve
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent ef987c0 commit 934b7b7

File tree

29 files changed

+277
-407
lines changed

29 files changed

+277
-407
lines changed

053_maximum_subarray/max_subarray.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
#include <stdlib.h>
33
#include <limits.h>
44

5-
static int recursive(int *nums, int lo, int hi)
5+
static int partition(int *nums, int lo, int hi)
66
{
77
if (lo == hi) {
88
return nums[lo];
99
}
1010

1111
int ce = (hi - lo) / 2;
12-
int left_max = recursive(nums, lo, lo + ce);
13-
int right_max = recursive(nums, hi - ce, hi);
12+
int left_max = partition(nums, lo, lo + ce);
13+
int right_max = partition(nums, hi - ce, hi);
1414

1515
int i;
1616
int left_border = 0, left_border_max = INT_MIN;
@@ -48,7 +48,7 @@ static int maxSubArray(int* nums, int numsSize)
4848
}
4949
return max;
5050
#else
51-
return recursive(nums, 0, numsSize - 1);
51+
return partition(nums, 0, numsSize - 1);
5252
#endif
5353
}
5454

054_spiral_matrix/spiral_matrix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ int main(int argc, char **argv)
6060
mat[i] = malloc(col * sizeof(int));
6161
for (j = 0; j < col; j++) {
6262
mat[i][j] = ++count;
63-
printf("%d ", mat[i][j]);
63+
printf("%d ", mat[i][j]);
6464
}
65-
printf("\n");
65+
printf("\n");
6666
}
6767
int *nums = spiralOrder(mat, row, col);
6868
for (i = 0; i < row * col; i++) {

055_jump_game/jump_game.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22
#include <stdlib.h>
33
#include <stdbool.h>
44

5-
static bool canJump(int* nums, int numsSize) {
6-
if (numsSize == 0) return false;
5+
static inline int max(int a, int b)
6+
{
7+
return a > b ? a : b;
8+
}
79

8-
int i = numsSize - 1, j;
9-
while (i > 0) {
10-
if (nums[--i] == 0) {
11-
for (j = i - 1; j >= 0; j--) {
12-
if (nums[j] > i - j) {
13-
break;
14-
}
15-
}
16-
if (j == -1) {
17-
return false;
18-
}
10+
static bool canJump(int* nums, int numsSize)
11+
{
12+
int i, pos = 0;
13+
for (i = 0; i < numsSize - 1; i++) {
14+
if (pos < i || pos >= numsSize - 1) {
15+
break;
1916
}
17+
pos = max(i + nums[i], pos);
2018
}
2119

22-
return true;
20+
return pos >= numsSize - 1;
2321
}
2422

2523
int main(int argc, char **argv)

060_permutation_sequence/permutation_sequence.c

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,34 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5-
static void reverse(int *a, int size)
5+
static int factorial(int n)
66
{
7-
int left = 0;
8-
int right = size - 1;
9-
while (left < right) {
10-
int tmp = a[left];
11-
a[left] = a[right];
12-
a[right] = tmp;
13-
left++;
14-
right--;
7+
if (n == 0) {
8+
return 0;
9+
} else if (n == 1) {
10+
return 1;
11+
} else {
12+
return n * factorial(n - 1);
1513
}
1614
}
1715

18-
void nextPermutation(int* nums, int numsSize) {
19-
if (numsSize <= 1) {
20-
return;
21-
}
22-
23-
int *p = nums + numsSize - 1;
24-
int *q = nums + numsSize - 1;
25-
26-
while (p != nums && *(p - 1) >= *p) {
27-
p--;
28-
}
29-
30-
if (p != nums) {
31-
int n = *(p - 1);
32-
while (*q <= n) {
33-
q--;
34-
}
35-
36-
int tmp = *q;
37-
*q = *(p - 1);
38-
*(p - 1) = tmp;
39-
}
40-
reverse(p, numsSize - (p - nums));
41-
}
42-
43-
static char* getPermutation(int n, int k) {
16+
static char* getPermutation(int n, int k)
17+
{
4418
int i;
4519
int *permutation = malloc(n * sizeof(int));
4620
for (i = 0; i < n; i++) {
4721
permutation[i] = i + 1;
4822
}
49-
while (--k > 0) {
50-
nextPermutation(permutation, n);
51-
}
23+
5224
char *result = malloc(n + 1);
5325
for (i = 0; i < n; i++) {
54-
result[i] = permutation[i] + '0';
26+
int fac = factorial(n - i - 1);
27+
int j = k > 1 ? (k - 1) / fac : 0;
28+
result[i] = permutation[j] + '0';
29+
k -= j * fac;
30+
memmove(permutation + j, permutation + j + 1, (n - j) * sizeof(int));
5531
}
5632
result[n] = '\0';
57-
free(permutation);
5833
return result;
5934
}
6035

062_unique_path/unique_path.c

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

4-
static void valid_path_recursive(int col, int m, int row, int n, int *count)
4+
static int uniquePaths(int m, int n)
55
{
6-
if (col == m - 1 && row == n - 1) {
7-
(*count)++;
8-
} else {
9-
if (m > n) {
10-
if (col < m - 1) {
11-
valid_path(col + 1, m, row, n, count);
12-
}
13-
if (row < n - 1) {
14-
valid_path(col, m, row + 1, n, count);
15-
}
16-
} else {
17-
if (col < m - 1) {
18-
valid_path(col + 1, m, row, n, count);
19-
}
20-
if (row < n - 1) {
21-
valid_path(col, m, row + 1, n, count);
22-
}
23-
}
24-
}
25-
}
26-
27-
static int uniquePaths(int m, int n) {
28-
//int count = 0;
29-
//valid_path(0, m, 0, n, &count);
30-
//return count;
316
int row, col;
327
int *grids = malloc(m * n * sizeof(int));
338
for (col = 0; col < m; col++) {
@@ -41,9 +16,7 @@ static int uniquePaths(int m, int n) {
4116
grids[row * m + col] = grids[row * m + col - 1] + grids[(row - 1) * m + col];
4217
}
4318
}
44-
int result = grids[m * n - 1];
45-
free(grids);
46-
return result;
19+
return grids[m * n - 1];
4720
}
4821

4922
int main(int argc, char **argv)

064_minumum_path_sum/minimum_path_sum.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <string.h>
4-
#include <limits.h>
54

6-
int minPathSum(int** grid, int gridRowSize, int gridColSize) {
5+
static inline int min(int a, int b)
6+
{
7+
return a < b ? a : b;
8+
}
9+
10+
int minPathSum(int** grid, int gridRowSize, int gridColSize)
11+
{
712
int i, j;
813
int **dp = malloc(gridRowSize * sizeof(int *));
914
for (i = 0; i < gridRowSize; i++) {
@@ -23,11 +28,9 @@ int minPathSum(int** grid, int gridRowSize, int gridColSize) {
2328
dp[0][i] = sum;
2429
}
2530

26-
int min = INT_MAX;
2731
for (i = 1; i < gridRowSize; i++) {
2832
for (j = 1; j < gridColSize; j++) {
29-
int n = dp[i - 1][j] < dp[i][j - 1] ? dp[i - 1][j] : dp[i][j - 1];
30-
dp[i][j] = n + grid[i][j];
33+
dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]);
3134
}
3235
}
3336

065_valid_number/valid_number.c

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,38 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <stdbool.h>
4+
#include <ctype.h>
45

5-
static bool isNumber(const char *s) {
6-
bool point = false;
7-
bool hasE = false;
8-
9-
//trim the space
10-
while(isspace(*s)) s++;
11-
//check empty
12-
if (*s == '\0' ) return false;
13-
//check sign
14-
if (*s=='+' || *s=='-') s++;
15-
16-
const char *head = s;
17-
for(; *s!='\0'; s++){
18-
// if meet point
19-
if ( *s == '.' ){
20-
if ( hasE == true || point == true){
21-
return false;
22-
}
23-
if ( s == head && !isdigit(*(s+1)) ){
24-
return false;
25-
}
26-
point = true;
27-
continue;
28-
}
29-
//if meet "e"
30-
if ( *s == 'e' ){
31-
if ( hasE == true || s == head) {
32-
return false;
33-
}
34-
s++;
35-
if ( *s=='+' || *s=='-' ) s++;
36-
if ( !isdigit(*s) ) return false;
37-
38-
hasE = true;
39-
continue;
40-
}
41-
//if meet space, check the rest chars are space or not
42-
if (isspace(*s)){
43-
for (; *s != '\0'; s++){
44-
if (!isspace(*s)) return false;
45-
}
46-
return true;
47-
}
48-
if ( !isdigit(*s) ) {
49-
return false;
6+
static bool isNumber(const char *s)
7+
{
8+
while (*s == ' ')
9+
++s;
10+
bool if_find_num = false;
11+
if (*s == '-' || *s == '+')
12+
++s;
13+
while (isdigit(*s)) {
14+
if_find_num = true;
15+
++s;
16+
}
17+
if (*s == '.')
18+
++s;
19+
while (isdigit(*s)) {
20+
if_find_num = true;
21+
++s;
22+
}
23+
if (if_find_num == true && *s == 'e') {
24+
++s;
25+
if (*s == '+' || *s == '-')
26+
++s;
27+
if_find_num = false;
28+
while (isdigit(*s)) {
29+
if_find_num = true;
30+
++s;
5031
}
51-
5232
}
53-
54-
return true;
33+
while (*s == ' ')
34+
++s;
35+
return *s == '\0' && if_find_num == true;
5536
}
5637

5738
int main(int argc, char** argv)

071_simplify_path/simplify_path.c

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

5-
static char* simplifyPath(char* path) {
5+
static char* simplifyPath(char* path)
6+
{
67
int len = strlen(path);
78
if (len == 0) {
89
return path;

072_edit_distance/edit_distance.c

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,6 @@
44
#include <string.h>
55

66
/*
7-
* Dynamic Programming
8-
*
9-
* Definitaion
10-
*
11-
* m[i][j] is minimal distance from word1[0..i] to word2[0..j]
12-
*
13-
* So,
14-
*
15-
* 1) if word1[i] == word2[j], then m[i][j] == m[i-1][j-1].
16-
*
17-
* 2) if word1[i] != word2[j], then we need to find which one below is minimal:
18-
*
19-
* min( m[i-1][j-1], m[i-1][j], m[i][j-1] )
20-
*
21-
* and +1 - current char need be changed.
22-
*
23-
* Let's take a look m[1][2] : "a" => "ab"
24-
*
25-
* +---+ +---+
26-
* ''=> a | 1 | | 2 | '' => ab
27-
* +---+ +---+
28-
*
29-
* +---+ +---+
30-
* a => a | 0 | | 1 | a => ab
31-
* +---+ +---+
32-
*
33-
* To know the minimal distance `a => ab`, we can get it from one of the following cases:
34-
*
35-
* 1) delete the last char in word1, minDistance( '' => ab ) + 1
36-
* 2) delete the last char in word2, minDistance( a => a ) + 1
37-
* 3) change the last char, minDistance( '' => a ) + 1
38-
*
39-
*
40-
* For Example:
41-
*
427
* word1="abb", word2="abccb"
438
*
449
* 1) Initialize the DP matrix as below:
@@ -56,9 +21,13 @@
5621
* a 1 0 1 2 3 4
5722
* b 2 1 0 1 2 3
5823
* b 3 2 1 1 2 2
59-
*
6024
*/
6125

26+
static inline int min(int a, int b)
27+
{
28+
return a < b ? a : b;
29+
}
30+
6231
static int minDistance(char* word1, char* word2)
6332
{
6433
int i, j;
@@ -82,9 +51,7 @@ static int minDistance(char* word1, char* word2)
8251
if (word1[i - 1] == word2[j - 1]) {
8352
dp[i][j] = dp[i - 1][j - 1];
8453
} else {
85-
int min = dp[i - 1][j] > dp[i][j - 1] ? dp[i][j - 1] : dp[i - 1][j];
86-
dp[i][j] = min > dp[i - 1][j - 1] ? dp[i - 1][j - 1] : min;
87-
dp[i][j] += 1;
54+
dp[i][j] = 1 + min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1]));
8855
}
8956
}
9057
}

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