Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a147a57

Browse files
Improvement
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 89d8e28 commit a147a57

File tree

17 files changed

+77
-59
lines changed

17 files changed

+77
-59
lines changed

005_longest_palindromic_substring/longest_palindromic_substring.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static inline int min(int a, int b)
1010
static int manacher(char *s, char output[])
1111
{
1212
int i, j;
13-
char s2[1000] = { '\0' };
13+
char s2[3000] = { '\0' };
1414

1515
s2[0] = '$';
1616
for (i = 0; s[i] != '\0'; i++) {
@@ -21,7 +21,7 @@ static int manacher(char *s, char output[])
2121
int len = (i<<1)+2;
2222
s2[len] = '\0';
2323

24-
int p[1000] = { 0 }; // 以s2中某一点为中心的回文半径
24+
int p[3000] = { 0 }; // 以s2中某一点为中心的回文半径
2525
int id = 0; // 回文的中心点
2626
int limit = 0; // 最长回文的右边界点
2727
int maxLen = 0; // 最大回文长度
@@ -67,7 +67,7 @@ static char *longestPalindrom(char *s)
6767
return s;
6868
}
6969

70-
char *palindrome = malloc(1000);
70+
char *palindrome = malloc(2000);
7171
memset(palindrome, 0, sizeof(palindrome));
7272

7373
int size = manacher(s, palindrome);

006_zigzag_conversion/zigzag_conversion.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* convert(char* s, int numRows) {
5+
static char* convert(char* s, int numRows)
6+
{
67
if (numRows <= 1) return s;
78

89
int len = strlen(s);

008_atoi/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test atoi.c

008_atoi/atoi.c

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
1-
/*
2-
* Copyright (C) 2015, Leo Ma <begeekmyfriend@gmail.com>
3-
*/
4-
51
#include <stdio.h>
62
#include <stdlib.h>
73
#include <limits.h>
84

9-
int main(int argc, char **argv)
5+
static int myAtoi(char* str)
106
{
11-
char *alpha, *s;
12-
int n = 0, sign = 0;
13-
14-
if (argc != 2) {
15-
printf("Usage: ./atoi 123\n");
16-
exit(-1);
17-
}
7+
char *s;
8+
int n = 0, sign = 0;
189

19-
alpha = argv[1];
20-
while (*alpha == ' ' || *alpha == '\t') {
21-
alpha++;
22-
}
10+
while (*str == ' ' || *str == '\t') {
11+
str++;
12+
}
2313

24-
for (s = alpha; *s != '\0'; s++) {
25-
if (isdigit(*s)) {
26-
int d = *s - '0';
27-
if (sign) {
28-
if (-n < (INT_MIN + d) / 10) {
29-
n = INT_MIN;
30-
break;
31-
}
32-
} else {
33-
if (n > (INT_MAX - d) / 10) {
34-
n = INT_MAX;
35-
break;
36-
}
37-
}
38-
n = n * 10 + d;
39-
} else if (*s == '-' && isdigit(*(s + 1))) {
40-
sign = 1;
41-
} else if (*s == '+' && isdigit(*(s + 1))) {
42-
sign = 0;
43-
} else {
44-
break;
14+
for (s = str; *s != '\0'; s++) {
15+
if (isdigit(*s)) {
16+
int d = *s - '0';
17+
if (sign) {
18+
if (-n < (INT_MIN + d) / 10) {
19+
n = INT_MIN;
20+
break;
21+
}
22+
} else {
23+
if (n > (INT_MAX - d) / 10) {
24+
n = INT_MAX;
25+
break;
4526
}
27+
}
28+
n = n * 10 + d;
29+
} else if (*s == '-' && isdigit(*(s + 1))) {
30+
sign = 1;
31+
} else if (*s == '+' && isdigit(*(s + 1))) {
32+
sign = 0;
33+
} else {
34+
break;
4635
}
36+
}
37+
38+
return sign ? -n : n;
39+
}
40+
41+
int main(int argc, char **argv)
42+
{
43+
if (argc != 2) {
44+
printf("Usage: ./atoi 123\n");
45+
exit(-1);
46+
}
4747

48-
n = sign ? -n : n;
49-
printf("n = %d %x\n", n, n);
48+
int n = myAtoi(argv[1]);
49+
printf("n = %d %x\n", n, n);
5050

51-
return 0;
51+
return 0;
5252
}

009_palindrome_number/palindrome_number.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ static int reverse(int x)
1212
return y;
1313
}
1414

15-
static bool isPalindrome(int x) {
15+
static bool isPalindrome(int x)
16+
{
1617
if (x == 0) {
1718
return true;
1819
}

010_regular_expression_matching/regular_expression.c

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

6-
bool isMatch(char* s, char* p) {
6+
static bool isMatch(char* s, char* p)
7+
{
78
if (*p == '\0') {
89
return *s == '\0';
910
}

012_roman_numeral/roman_numeral.c

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

5-
static void num2char(char **num, int bit, int n) {
5+
static void num2char(char **num, int bit, int n)
6+
{
67
int i;
78
char low, mid, high;
89
char *p = *num;
@@ -61,7 +62,8 @@ static void num2char(char **num, int bit, int n) {
6162

6263
static char roman_numeral[64];
6364

64-
static char *intToRoman(int num) {
65+
static char *intToRoman(int num)
66+
{
6567
char *p = &roman_numeral[0];
6668
int thousand_bit = num / 1000;
6769
int hundred_bit = (num % 1000) / 100;

013_roman_to_integer/roman_to_integer.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ static int roman_to_integer(char c)
2323
}
2424
}
2525

26-
int romanToInt (char *s) {
26+
int romanToInt (char *s)
27+
{
2728
int i, result = roman_to_integer(s[0]);
2829

2930
for (i = 1; s[i] != '\0'; i++) {
@@ -39,6 +40,7 @@ int romanToInt (char *s) {
3940

4041
return result;
4142
}
43+
4244
int main(int argc, char **argv)
4345
{
4446
if (argc < 2) {

014_longest_common_prefix/common_prefix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
static char* longestCommonPrefix(char** strs, int strsSize)
66
{
77
int i, count = 0;
8-
char *result = malloc(100);
8+
char *result = malloc(1000);
99
while (strsSize > 0) {
1010
char c = strs[0][count];
1111
for (i = 1; i < strsSize; i++) {

017_letter_combinations_of_a_phone_number/letter_combinations.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
** Return an array of size *returnSize.
77
** Note: The returned array must be malloced, assume caller calls free().
88
**/
9-
char** letterCombinations(char* digits, int* returnSize) {
9+
static char** letterCombinations(char* digits, int* returnSize)
10+
{
1011
char *letter_matrix[10];
1112
letter_matrix[0] = "";
1213
letter_matrix[1] = " ";

029_divide_two_integers/divide.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 <limits.h>
44

5-
int divide(int dividend, int divisor) {
5+
int divide(int dividend, int divisor)
6+
{
67
int sign = (float) dividend / divisor > 0 ? 1 : -1;
78
unsigned int dvd = dividend > 0 ? dividend : -dividend;
89
unsigned int dvs = divisor > 0 ? divisor : -divisor;

034_search_for_a_range/range_search.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ static int binary_search_end(int *a, int size, int target)
4545
** Return an array of size *returnSize.
4646
** Note: The returned array must be malloced, assume caller calls free().
4747
**/
48-
int* searchRange(int* nums, int numsSize, int target, int* returnSize) {
48+
static int* searchRange(int* nums, int numsSize, int target, int* returnSize)
49+
{
4950
int *range = malloc(2 * sizeof(int));
5051
*returnSize = 2;
5152

035_search_insert_position/insert_position.c

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

4-
static int searchInsert(int* nums, int numsSize, int target) {
4+
static int searchInsert(int* nums, int numsSize, int target)
5+
{
56
int low = -1;
67
int high = numsSize;
78
while (low + 1 < high) {

036_valid_sudoku/valid_sudoku.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ static bool valid(char **board, int row, int col)
6767
return true;
6868
}
6969

70-
bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
70+
static bool isValidSudoku(char** board, int boardRowSize, int boardColSize)
71+
{
7172
if (boardRowSize != 9 || boardColSize != 9) {
7273
return false;
7374
}

041_first_missing_positive/missing_positive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4-
static void swap(int *a, int *b)
4+
static inline void swap(int *a, int *b)
55
{
66
int tmp = *a;
77
*a = *b;

042_trapping_rain_water/trap_water.c

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

4-
static int trap(int* height, int heightSize) {
4+
static int trap(int* height, int heightSize)
5+
{
56
if (heightSize < 2) {
67
return 0;
78
}

048_rotate_image/rotate.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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 matrixRowSize, int matrixColSize)
5+
{
56
int i, j;
67
if (matrixRowSize != matrixColSize) {
78
return;
@@ -32,8 +33,10 @@ int main(int argc, char **argv)
3233
for (i = 0; i < row_size; i++) {
3334
matrix[i] = malloc(col_size * sizeof(int));
3435
for (j = 0; j < col_size; j++) {
35-
matrix[i][j] = count++;
36+
matrix[i][j] = ++count;
37+
printf("%d ", matrix[i][j]);
3638
}
39+
printf("\n");
3740
}
3841

3942
rotate(matrix, row_size, col_size);

0 commit comments

Comments
 (0)