Skip to content

Commit d507060

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

File tree

32 files changed

+366
-480
lines changed

32 files changed

+366
-480
lines changed

005_longest_palindromic_substring/longest_palindromic_substring.c

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,61 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5-
static void find(char *s, int len, int low, int high, int *max_size, char *palindrome) {
6-
while (low >= 0 && high < len && s[low] == s[high]) {
7-
low--;
8-
high++;
9-
}
10-
low++;
11-
high--;
5+
static inline int min(int a, int b)
6+
{
7+
return a < b ? a : b;
8+
}
9+
10+
static int manacher(char *s, char output[])
11+
{
12+
int i, j;
13+
char s2[1000] = { '\0' };
1214

13-
if (high - low + 1 > *max_size) {
14-
*max_size = high - low + 1;
15-
memcpy(palindrome, s + low, *max_size);
15+
s2[0] = '$';
16+
for (i = 0; s[i] != '\0'; i++) {
17+
s2[(i<<1)+1] = '#';
18+
s2[(i<<1)+2] = s[i];
1619
}
20+
s2[(i<<1)+1] = '#';
21+
int len = (i<<1)+2;
22+
s2[len] = '\0';
23+
24+
int p[1000] = { 0 }; // 以s2中某一点为中心的回文半径
25+
int id = 0; // 回文的中心点
26+
int limit = 0; // 最长回文的右边界点
27+
int maxLen = 0; // 最大回文长度
28+
int maxId = 0; // 最长回文的中心点
29+
for (i = 1; i < len; i++) {
30+
if (i < limit) {
31+
p[i] = min(p[2*id-i], limit-i);
32+
} else {
33+
p[i] = 1;
34+
}
35+
36+
while (s2[i+p[i]] == s2[i-p[i]]) {
37+
p[i]++;
38+
}
39+
40+
if (i+p[i] > limit) {
41+
limit = i+p[i];
42+
id = i;
43+
}
44+
if (maxLen < p[i]-1) {
45+
maxLen = p[i]-1;
46+
maxId = i;
47+
}
48+
}
49+
50+
for (j = 0, i = maxId - maxLen; i <= maxId+maxLen; i++) {
51+
if (s2[i] != '#') {
52+
output[j++] = s2[i];
53+
}
54+
}
55+
return maxLen;
1756
}
1857

19-
static char *longestPalindrom(char *s) {
58+
static char *longestPalindrom(char *s)
59+
{
2060
int i;
2161
if (s == NULL) {
2262
return NULL;
@@ -30,13 +70,8 @@ static char *longestPalindrom(char *s) {
3070
char *palindrome = malloc(1000);
3171
memset(palindrome, 0, sizeof(palindrome));
3272

33-
int max_size = 0;
34-
for (i = 0; i < len; i++) {
35-
/* start from the middle and scan both two sides */
36-
find(s, len, i, i, &max_size, palindrome);
37-
find(s, len, i, i + 1, &max_size, palindrome);
38-
}
39-
73+
int size = manacher(s, palindrome);
74+
palindrome[size] = '\0';
4075
return palindrome;
4176
}
4277

070_climbing_stairs/climb_stairs.c

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

5-
static int recursive(int n, int *count)
5+
static int dfs(int n, int *count)
66
{
77
if (n == 0) {
88
return 0;
99
} else if (count[n] > 0) {
1010
return count[n];
1111
} else {
1212
if (n >= 1) {
13-
count[n] += recursive(n - 1, count);
13+
count[n] += dfs(n - 1, count);
1414
}
1515
if (n >= 2) {
16-
count[n] += recursive(n - 2, count);
16+
count[n] += dfs(n - 2, count);
1717
}
1818
return count[n];
1919
}
@@ -25,7 +25,7 @@ static int climbStairs(int n)
2525
memset(count, 0, (n + 1) * sizeof(int));
2626
count[1] = 1;
2727
count[2] = 2;
28-
return recursive(n, count);
28+
return dfs(n, count);
2929
}
3030

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

087_scramble_string/scramble_string.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ static bool scramble(char *s1, int low1, int high1, char *s2, int low2, int high
4949
}
5050
}
5151

52-
static bool isScramble(char* s1, char* s2) {
53-
int len1 = strlen(s1);
54-
int len2 = strlen(s2);
55-
return scramble(s1, 0, len1 - 1, s2, 0, len2 - 1);
52+
static bool isScramble(char* s1, char* s2)
53+
{
54+
return scramble(s1, 0, strlen(s1) - 1, s2, 0, strlen(s2) - 1);
5655
}
5756

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

105_construct_binary_tree_from_preorder_and_inorder_traversal/binary_tree_build.c

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4+
#define container_of(ptr, type, member) \
5+
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
6+
7+
#define list_entry(ptr, type, member) \
8+
container_of(ptr, type, member)
9+
10+
#define hlist_for_each(pos, head) \
11+
for (pos = (head)->first; pos; pos = pos->next)
12+
413
struct hlist_node;
514

615
struct hlist_head {
@@ -15,11 +24,6 @@ static inline void INIT_HLIST_HEAD(struct hlist_head *h) {
1524
h->first = NULL;
1625
}
1726

18-
static inline void INIT_HLIST_NODE(struct hlist_node *n) {
19-
n->next = NULL;
20-
n->pprev = NULL;
21-
}
22-
2327
static inline int hlist_empty(struct hlist_head *h) {
2428
return !h->first;
2529
}
@@ -44,15 +48,6 @@ static inline void hlist_del(struct hlist_node *n)
4448
}
4549
}
4650

47-
#define container_of(ptr, type, member) \
48-
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
49-
50-
#define list_entry(ptr, type, member) \
51-
container_of(ptr, type, member)
52-
53-
#define hlist_for_each(pos, head) \
54-
for (pos = (head)->first; pos; pos = pos->next)
55-
5651
struct TreeNode {
5752
int val;
5853
struct TreeNode *left;
@@ -87,18 +82,17 @@ static struct TreeNode *node_new(int val)
8782
return tn;
8883
}
8984

90-
static struct TreeNode *recursive(int *preorder, int pre_low, int pre_high,
91-
int *inorder, int in_low, int in_high,
92-
struct hlist_head *in_heads, int size)
85+
static struct TreeNode *dfs(int *preorder, int pre_low, int pre_high, int *inorder,
86+
int in_low, int in_high, struct hlist_head *in_heads, int size)
9387
{
9488
if (in_low > in_high || pre_low > pre_high) {
9589
return NULL;
9690
}
9791
struct TreeNode *tn = malloc(sizeof(*tn));
9892
tn->val = preorder[pre_low];
9993
int index = find(preorder[pre_low], size, in_heads);
100-
tn->left = recursive(preorder, pre_low + 1, pre_low + (index - in_low), inorder, in_low, index - 1, in_heads, size);
101-
tn->right = recursive(preorder, pre_high - (in_high - index - 1), pre_high, inorder, index + 1, in_high, in_heads, size);
94+
tn->left = dfs(preorder, pre_low + 1, pre_low + (index - in_low), inorder, in_low, index - 1, in_heads, size);
95+
tn->right = dfs(preorder, pre_high - (in_high - index - 1), pre_high, inorder, index + 1, in_high, in_heads, size);
10296
return tn;
10397
}
10498

@@ -122,49 +116,7 @@ static struct TreeNode *buildTree(int *preorder, int preorderSize, int *inorder,
122116
node_add(inorder[i], i, inorderSize, in_heads);
123117
}
124118

125-
#if 0
126-
struct hlist_head *pre_heads = malloc(preorderSize * sizeof(*pre_heads));
127-
for (i = 0; i < preorderSize; i++) {
128-
INIT_HLIST_HEAD(&pre_heads[i]);
129-
}
130-
for (i = 0; i < inorderSize; i++) {
131-
node_add(preorder[i], i, preorderSize, pre_heads);
132-
}
133-
134-
int last_index, level = 0;
135-
struct TreeNode **stack = malloc(preorderSize * sizeof(*stack));
136-
struct TreeNode *tn, *root = NULL;
137-
for (i = 0; i < preorderSize; i++) {
138-
if (i == 0) {
139-
tn = root = node_new(preorder[0]);
140-
last_index = find(preorder[0], inorderSize, in_heads);
141-
stack[level++] = root;
142-
} else {
143-
int index = find(preorder[i], inorderSize, in_heads);
144-
if (index < last_index) {
145-
tn->left = node_new(preorder[i]);
146-
tn = tn->left;
147-
} else {
148-
for (j = index - 1; j >= 0; j--) {
149-
if (find(inorder[j], preorderSize, pre_heads) < i) {
150-
break;
151-
}
152-
}
153-
/* find the parent of the right child */
154-
while (stack[--level]->val != inorder[j]) {}
155-
tn = stack[level++];
156-
tn->right = node_new(preorder[i]);
157-
tn = tn->right;
158-
}
159-
stack[level++] = tn;
160-
last_index = index;
161-
}
162-
}
163-
164-
return root;
165-
#else
166-
return recursive(preorder, 0, preorderSize - 1, inorder, 0, inorderSize - 1, in_heads, inorderSize);
167-
#endif
119+
return dfs(preorder, 0, preorderSize - 1, inorder, 0, inorderSize - 1, in_heads, inorderSize);
168120
}
169121

170122
static void dump(struct TreeNode *node)

106_construct_binary_tree_from_inorder_and_postorder_traversal/binary_tree_build.c

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4+
#define container_of(ptr, type, member) \
5+
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
6+
7+
#define list_entry(ptr, type, member) \
8+
container_of(ptr, type, member)
9+
10+
#define hlist_for_each(pos, head) \
11+
for (pos = (head)->first; pos; pos = pos->next)
12+
413
struct hlist_node;
514

615
struct hlist_head {
@@ -15,11 +24,6 @@ static inline void INIT_HLIST_HEAD(struct hlist_head *h) {
1524
h->first = NULL;
1625
}
1726

18-
static inline void INIT_HLIST_NODE(struct hlist_node *n) {
19-
n->next = NULL;
20-
n->prev = NULL;
21-
}
22-
2327
static inline int hlist_empty(struct hlist_head *h) {
2428
return !h->first;
2529
}
@@ -44,15 +48,6 @@ static inline void hlist_del(struct hlist_node *n)
4448
}
4549
}
4650

47-
#define container_of(ptr, type, member) \
48-
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
49-
50-
#define list_entry(ptr, type, member) \
51-
container_of(ptr, type, member)
52-
53-
#define hlist_for_each(pos, head) \
54-
for (pos = (head)->first; pos; pos = pos->next)
55-
5651
struct TreeNode {
5752
int val;
5853
struct TreeNode *left;
@@ -96,25 +91,23 @@ static void node_add(int val, int index, int size, struct hlist_head *heads)
9691
hlist_add_head(&on->node, &heads[hash]);
9792
}
9893

99-
static struct TreeNode *recursive(int *inorder, int in_low, int in_high,
100-
int *postorder, int post_low, int post_high,
101-
struct hlist_head *in_heads, int size)
94+
static struct TreeNode *dfs(int *inorder, int in_low, int in_high, int *postorder,
95+
int post_low, int post_high, struct hlist_head *in_heads, int size)
10296
{
10397
if (in_low > in_high || post_low > post_high) {
10498
return NULL;
10599
}
106100
struct TreeNode *tn = malloc(sizeof(*tn));
107101
tn->val = postorder[post_high];
108102
int index = find(postorder[post_high], size, in_heads);
109-
tn->left = recursive(inorder, in_low, index - 1, postorder, post_low, post_low + (index - 1 - in_low), in_heads, size);
110-
tn->right = recursive(inorder, index + 1, in_high, postorder, post_high - (in_high - index), post_high - 1, in_heads, size);
103+
tn->left = dfs(inorder, in_low, index - 1, postorder, post_low, post_low + (index - 1 - in_low), in_heads, size);
104+
tn->right = dfs(inorder, index + 1, in_high, postorder, post_high - (in_high - index), post_high - 1, in_heads, size);
111105
return tn;
112106
}
113107

114108
static struct TreeNode *buildTree(int *inorder, int inorderSize, int *postorder, int postorderSize)
115109
{
116110
int i, j;
117-
118111
struct hlist_head *in_heads = malloc(inorderSize * sizeof(*in_heads));
119112
for (i = 0; i < inorderSize; i++) {
120113
INIT_HLIST_HEAD(&in_heads[i]);
@@ -123,48 +116,7 @@ static struct TreeNode *buildTree(int *inorder, int inorderSize, int *postorder,
123116
node_add(inorder[i], i, inorderSize, in_heads);
124117
}
125118

126-
#if 0
127-
struct hlist_head *post_heads = malloc(postorderSize * sizeof(*post_heads));
128-
for (i = 0; i < postorderSize; i++) {
129-
INIT_HLIST_HEAD(&post_heads[i]);
130-
}
131-
for (i = 0; i < inorderSize; i++) {
132-
node_add(postorder[i], i, postorderSize, post_heads);
133-
}
134-
int last_index, level = 0;
135-
struct TreeNode **stack = malloc(postorderSize * sizeof(*stack));
136-
struct TreeNode *tn, *root = NULL;
137-
for (i = postorderSize - 1; i >= 0; i--) {
138-
if (i == postorderSize - 1) {
139-
tn = root = node_new(postorder[i]);
140-
last_index = find(postorder[i], inorderSize, in_heads);
141-
stack[level++] = root;
142-
} else {
143-
int index = find(postorder[i], inorderSize, in_heads);
144-
if (index > last_index) {
145-
tn->right = node_new(postorder[i]);
146-
tn = tn->right;
147-
} else {
148-
for (j = index + 1; j < inorderSize; j++) {
149-
if (find(inorder[j], postorderSize, post_heads) > i) {
150-
break;
151-
}
152-
}
153-
/* find the parent of the left child */
154-
while (stack[--level]->val != inorder[j]) {}
155-
tn = stack[level++];
156-
tn->left = node_new(postorder[i]);
157-
tn = tn->left;
158-
}
159-
stack[level++] = tn;
160-
last_index = index;
161-
}
162-
}
163-
164-
return root;
165-
#else
166-
return recursive(inorder, 0, inorderSize - 1, postorder, 0, postorderSize - 1, in_heads, inorderSize);
167-
#endif
119+
return dfs(inorder, 0, inorderSize - 1, postorder, 0, postorderSize - 1, in_heads, inorderSize);
168120
}
169121

170122
static void dump(struct TreeNode *node)

0 commit comments

Comments
 (0)