Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e58c64e

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 8bad1f6 commit e58c64e

File tree

4 files changed

+60
-171
lines changed

4 files changed

+60
-171
lines changed

‎049_group_anagrams/anagrams.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static char*** groupAnagrams(char** strs, int strsSize, int* returnSize, int** r
4343
qsort(words[i], len, sizeof(char), compare);
4444
int hash = BKDRHash(words[i], hash_size);
4545
/* find available hash bucket */
46-
for (j = hash; ht[j].num > 0 && strcmp(ht[j].word, words[i]); j = ++j % hash_size) {}
46+
for (j = hash; ht[j].num > 0 && strcmp(ht[j].word, words[i]); j = (j+1) % hash_size) {}
4747
if (ht[j].num == 0) {
4848
ht[j].word = words[i];
4949
count++;
@@ -52,7 +52,6 @@ static char*** groupAnagrams(char** strs, int strsSize, int* returnSize, int** r
5252
}
5353

5454
int k = 0;
55-
struct hlist_node *p;
5655
char ***lists = malloc(count * sizeof(char **));
5756
*returnColumnSizes = malloc(count * sizeof(int));
5857
for (i = 0; i < hash_size; i++) {
@@ -74,7 +73,7 @@ static char*** groupAnagrams(char** strs, int strsSize, int* returnSize, int** r
7473
int main(int argc, char **argv)
7574
{
7675
int *column_sizes, count = 0, i, j;
77-
char ***lists = groupAnagrams(argv + 1, argc - 1, &column_sizes, &count);
76+
char ***lists = groupAnagrams(argv + 1, argc - 1, &count, &column_sizes);
7877
for (i = 0; i < count; i++) {
7978
for (j = 0; j < column_sizes[i]; j++) {
8079
printf("%s ", lists[i][j]);

‎126_word_ladder_ii/word_ladder.c‎

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,6 @@
1717
#define list_for_each_safe(p, n, head) \
1818
for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
1919

20-
#define hlist_for_each(pos, head) \
21-
for (pos = (head)->first; pos; pos = pos->next)
22-
23-
#define hlist_for_each_safe(pos, n, head) \
24-
for (pos = (head)->first; pos && ({ n = pos->next; true; }); pos = n)
25-
26-
struct hlist_node;
27-
28-
struct hlist_head {
29-
struct hlist_node *first;
30-
};
31-
32-
struct hlist_node {
33-
struct hlist_node *next, **pprev;
34-
};
35-
36-
static inline void INIT_HLIST_HEAD(struct hlist_head *h) {
37-
h->first = NULL;
38-
}
39-
40-
static inline int hlist_empty(struct hlist_head *h) {
41-
return !h->first;
42-
}
43-
44-
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
45-
{
46-
if (h->first != NULL) {
47-
h->first->pprev = &n->next;
48-
}
49-
n->next = h->first;
50-
n->pprev = &h->first;
51-
h->first = n;
52-
}
53-
5420
struct list_head {
5521
struct list_head *next, *prev;
5622
};
@@ -98,7 +64,7 @@ static inline void list_del(struct list_head *entry)
9864
struct word_node {
9965
int step;
10066
char *word;
101-
struct hlist_node node;
67+
struct list_head node;
10268
};
10369

10470
struct word_tree {
@@ -121,11 +87,11 @@ static int BKDRHash(char* str, int size)
12187
return hash % size;
12288
}
12389

124-
static struct word_node *find(char *word, struct hlist_head*hhead, int size, int step)
90+
static struct word_node *find(char *word, struct list_head*hheads, int size, int step)
12591
{
126-
struct hlist_node *p;
92+
struct list_head *p;
12793
int hash = BKDRHash(word, size);
128-
hlist_for_each(p, &hhead[hash]) {
94+
list_for_each(p, &hheads[hash]) {
12995
struct word_node *node = list_entry(p, struct word_node, node);
13096
if (!strcmp(node->word, word)) {
13197
if (node->step == 0 || node->step == step) {
@@ -160,9 +126,9 @@ static char*** findLadders(char* beginWord, char* endWord, char** wordList, int
160126
int hashsize = wordListSize * 2;
161127
char *word = malloc(len + 1);
162128

163-
struct hlist_head*hhead = malloc(hashsize * sizeof(*hhead));
129+
struct list_head*hheads = malloc(hashsize * sizeof(*hheads));
164130
for (i = 0; i < hashsize; i++) {
165-
INIT_HLIST_HEAD(hhead + i);
131+
INIT_LIST_HEAD(hheads + i);
166132
}
167133

168134
struct list_head *level_heads = malloc(wordListSize * sizeof(*level_heads));
@@ -177,7 +143,7 @@ static char*** findLadders(char* beginWord, char* endWord, char** wordList, int
177143
node->word = wordList[i];
178144
node->step = 0;
179145
int hash = BKDRHash(wordList[i], hashsize);
180-
hlist_add_head(&node->node, &hhead[hash]);
146+
list_add(&node->node, &hheads[hash]);
181147
}
182148

183149
/* FIFO */
@@ -193,7 +159,7 @@ static char*** findLadders(char* beginWord, char* endWord, char** wordList, int
193159
root->parents = malloc(sizeof(void *));
194160
root->parents[0] = NULL;
195161
list_add_tail(&root->sibling, &level_heads[0]);
196-
node = find(beginWord, hhead, hashsize, 1);
162+
node = find(beginWord, hheads, hashsize, 1);
197163
if (node != NULL) {
198164
node->step = 1;
199165
}
@@ -207,7 +173,7 @@ static char*** findLadders(char* beginWord, char* endWord, char** wordList, int
207173
char o = word[i];
208174
for (c = 'a'; c <= 'z'; c++) {
209175
word[i] = c;
210-
node = find(word, hhead, hashsize, first->step + 1);
176+
node = find(word, hheads, hashsize, first->step + 1);
211177
if (node != NULL) {
212178
int enqueue = 1;
213179
list_for_each(p, &level_heads[first->step]) {

‎127_word_ladder/word_ladder.c‎

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,6 @@
1717
#define list_for_each_safe(p, n, head) \
1818
for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
1919

20-
#define hlist_for_each(pos, head) \
21-
for (pos = (head)->first; pos; pos = pos->next)
22-
23-
#define hlist_for_each_safe(pos, n, head) \
24-
for (pos = (head)->first; pos && ({ n = pos->next; true; }); pos = n)
25-
26-
struct hlist_node;
27-
28-
struct hlist_head {
29-
struct hlist_node *first;
30-
};
31-
32-
struct hlist_node {
33-
struct hlist_node *next, **pprev;
34-
};
35-
36-
static inline void INIT_HLIST_HEAD(struct hlist_head *h) {
37-
h->first = NULL;
38-
}
39-
40-
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
41-
{
42-
if (h->first != NULL) {
43-
h->first->pprev = &n->next;
44-
}
45-
n->next = h->first;
46-
n->pprev = &h->first;
47-
h->first = n;
48-
}
49-
5020
struct list_head {
5121
struct list_head *next, *prev;
5222
};
@@ -94,7 +64,7 @@ static inline void list_del(struct list_head *entry)
9464
struct word_node {
9565
int step;
9666
char *word;
97-
struct hlist_node node;
67+
struct list_head node;
9868
struct list_head link;
9969
};
10070

@@ -108,11 +78,11 @@ static int BKDRHash(char* str, int size)
10878
return hash % size;
10979
}
11080

111-
static struct word_node *find(char *word, struct hlist_head*hhead, int size)
81+
static struct word_node *find(char *word, struct list_head*hheads, int size)
11282
{
113-
struct hlist_node *p;
83+
struct list_head *p;
11484
int hash = BKDRHash(word, size);
115-
hlist_for_each(p, &hhead[hash]) {
85+
list_for_each(p, &hheads[hash]) {
11686
struct word_node *node = list_entry(p, struct word_node, node);
11787
if (node->step == 0 && !strcmp(node->word, word)) {
11888
return node;
@@ -128,9 +98,9 @@ static int ladderLength(char* beginWord, char* endWord, char** wordList, int wor
12898
struct list_head queue;
12999
struct word_node *node;
130100

131-
struct hlist_head*hhead = malloc(wordListSize * sizeof(*hhead));
101+
struct list_head*hheads = malloc(wordListSize * sizeof(*hheads));
132102
for (i = 0; i < wordListSize; i++) {
133-
INIT_HLIST_HEAD(hhead + i);
103+
INIT_LIST_HEAD(hheads + i);
134104
}
135105

136106
/* Add into hash list */
@@ -139,7 +109,7 @@ static int ladderLength(char* beginWord, char* endWord, char** wordList, int wor
139109
node->word = wordList[i];
140110
node->step = 0;
141111
int hash = BKDRHash(wordList[i], wordListSize);
142-
hlist_add_head(&node->node, &hhead[hash]);
112+
list_add(&node->node, &hheads[hash]);
143113
}
144114

145115
/* FIFO */
@@ -157,7 +127,7 @@ static int ladderLength(char* beginWord, char* endWord, char** wordList, int wor
157127
for (c = 'a'; c <= 'z'; c++) {
158128
if (c == o) continue;
159129
word[i] = c;
160-
node = find(word, hhead, wordListSize);
130+
node = find(word, hheads, wordListSize);
161131
if (node != NULL) {
162132
list_add_tail(&node->link, &queue);
163133
node->step = first->step + 1;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /