Skip to content

Commit ce13413

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

File tree

8 files changed

+203
-22
lines changed

8 files changed

+203
-22
lines changed
File renamed without changes.
File renamed without changes.

133_clone_graph/clone_graph.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ static inline void INIT_HLIST_HEAD(struct hlist_head *h) {
2929
h->first = NULL;
3030
}
3131

32-
static inline void INIT_HLIST_NODE(struct hlist_node *n) {
33-
n->next = NULL;
34-
n->pprev = NULL;
35-
}
36-
3732
static inline int hlist_empty(struct hlist_head *h) {
3833
return !h->first;
3934
}

149_max_points_on_a_line/points_on_line.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
#include <stdbool.h>
44
#include <string.h>
55

6+
#define container_of(ptr, type, member) \
7+
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
8+
9+
#define list_entry(ptr, type, member) \
10+
container_of(ptr, type, member)
11+
12+
#define hlist_for_each(pos, head) \
13+
for (pos = (head)->first; pos; pos = pos->next)
14+
15+
#define hlist_for_each_safe(pos, n, head) \
16+
for (pos = (head)->first; pos && ({ n = pos->next; true; }); pos = n)
17+
618
struct hlist_node;
719

820
struct hlist_head {
@@ -17,11 +29,6 @@ static inline void INIT_HLIST_HEAD(struct hlist_head *h) {
1729
h->first = NULL;
1830
}
1931

20-
static inline void INIT_HLIST_NODE(struct hlist_node *n) {
21-
n->next = NULL;
22-
n->pprev = NULL;
23-
}
24-
2532
static inline int hlist_empty(struct hlist_head *h) {
2633
return !h->first;
2734
}
@@ -46,18 +53,6 @@ static inline void hlist_del(struct hlist_node *n)
4653
}
4754
}
4855

49-
#define container_of(ptr, type, member) \
50-
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
51-
52-
#define list_entry(ptr, type, member) \
53-
container_of(ptr, type, member)
54-
55-
#define hlist_for_each(pos, head) \
56-
for (pos = (head)->first; pos; pos = pos->next)
57-
58-
#define hlist_for_each_safe(pos, n, head) \
59-
for (pos = (head)->first; pos && ({ n = pos->next; true; }); pos = n)
60-
6156
struct Point {
6257
int x, y;
6358
};

207_course_schedule/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 course_schedule.c

207_course_schedule/course_schedule.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
#include <string.h>
5+
6+
enum {
7+
RET_FAILURE = -1,
8+
RET_OK,
9+
RET_IGNORE,
10+
};
11+
12+
struct graph_node {
13+
int req_num;
14+
int reqs[15];
15+
};
16+
17+
#define N 2000
18+
static struct graph_node courses[N];
19+
static bool takens[N];
20+
static bool touched[N];
21+
22+
static int dfs(struct graph_node *courses, int id, bool *takens, bool *touched)
23+
{
24+
int i;
25+
if (touched[id]) {
26+
return RET_IGNORE;
27+
} else if (takens[id]) {
28+
return RET_FAILURE;
29+
} else {
30+
takens[id] = true;
31+
for (i = 0; i < courses[id].req_num; i++) {
32+
int ret = dfs(courses, courses[id].reqs[i], takens, touched);
33+
if (ret == RET_FAILURE) {
34+
return ret;
35+
}
36+
}
37+
/* marked as available and no need to traverse next time */
38+
touched[id] = true;
39+
takens[id] = false;
40+
return RET_OK;
41+
}
42+
}
43+
44+
static bool canFinish(int numCourses, int** prerequisites, int prerequisitesRowSize, int prerequisitesColSize)
45+
{
46+
int i;
47+
memset(courses, 0, numCourses * sizeof(*courses));
48+
memset(takens, false, numCourses * sizeof(bool));
49+
memset(touched, false, numCourses * sizeof(bool));
50+
51+
for (i = 0; i < prerequisitesRowSize; i++) {
52+
int id = prerequisites[i][0];
53+
int req = prerequisites[i][1];
54+
courses[id].reqs[courses[id].req_num++] = req;
55+
}
56+
57+
for (i = 0; i < numCourses; i++) {
58+
if (dfs(courses, i, takens, touched) < 0) {
59+
return false;
60+
}
61+
}
62+
63+
return true;
64+
}
65+
66+
int main(void)
67+
{
68+
int i, course_num = 6, pair_num = 6;
69+
int **pairs = malloc(pair_num * sizeof(int *));
70+
pairs[0] = malloc(2 * sizeof(int));
71+
pairs[0][0] = 1;
72+
pairs[0][1] = 0;
73+
pairs[1] = malloc(2 * sizeof(int));
74+
pairs[1][0] = 2;
75+
pairs[1][1] = 1;
76+
pairs[2] = malloc(2 * sizeof(int));
77+
pairs[2][0] = 3;
78+
pairs[2][1] = 2;
79+
pairs[3] = malloc(2 * sizeof(int));
80+
pairs[3][0] = 1;
81+
pairs[3][1] = 3;
82+
pairs[4] = malloc(2 * sizeof(int));
83+
pairs[4][0] = 4;
84+
pairs[4][1] = 0;
85+
pairs[5] = malloc(2 * sizeof(int));
86+
pairs[5][0] = 0;
87+
pairs[5][1] = 5;
88+
printf("%s\n", canFinish(course_num, pairs, pair_num, 2) ? "true" : "false");
89+
return 0;
90+
}

210_course_schedule_ii/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 course_schedule.c
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
#include <string.h>
5+
6+
enum {
7+
RET_FAILURE = -1,
8+
RET_OK,
9+
RET_IGNORE,
10+
};
11+
12+
struct graph_node {
13+
int req_num;
14+
int reqs[15];
15+
};
16+
17+
static int dfs(struct graph_node *courses, int id, bool *takens, bool *touched, int *order, int *count)
18+
{
19+
int i;
20+
if (touched[id]) {
21+
return RET_IGNORE;
22+
} else if (takens[id]) {
23+
return RET_FAILURE;
24+
} else {
25+
takens[id] = true;
26+
for (i = 0; i < courses[id].req_num; i++) {
27+
int ret = dfs(courses, courses[id].reqs[i], takens, touched, order, count);
28+
if (ret == RET_FAILURE) {
29+
return ret;
30+
}
31+
}
32+
/* marked as available and no need to traverse next time */
33+
order[(*count)++] = id;
34+
touched[id] = true;
35+
takens[id] = false;
36+
return RET_OK;
37+
}
38+
}
39+
40+
#define N 2000
41+
static int order[N];
42+
static struct graph_node courses[N];
43+
static bool takens[N];
44+
static bool touched[N];
45+
46+
static int *findOrder(int numCourses, int** prerequisites, int prerequisitesRowSize, int prerequisitesColSize, int *returnSize)
47+
{
48+
int i;
49+
memset(courses, 0, numCourses * sizeof(*courses));
50+
memset(takens, false, numCourses * sizeof(bool));
51+
memset(touched, false, numCourses * sizeof(bool));
52+
53+
for (i = 0; i < prerequisitesRowSize; i++) {
54+
int id = prerequisites[i][0];
55+
int req = prerequisites[i][1];
56+
courses[id].reqs[courses[id].req_num++] = req;
57+
}
58+
59+
*returnSize = 0;
60+
for (i = 0; i < numCourses; i++) {
61+
if (dfs(courses, i, takens, touched, order, returnSize) < 0) {
62+
*returnSize = 0;
63+
return order;
64+
}
65+
}
66+
67+
return order;
68+
}
69+
70+
int main(void)
71+
{
72+
int i, course_num = 3, pair_num = 1;
73+
int **pairs = malloc(pair_num * sizeof(int *));
74+
pairs[0] = malloc(2 * sizeof(int));
75+
pairs[0][0] = 1;
76+
pairs[0][1] = 0;
77+
//pairs[1] = malloc(2 * sizeof(int));
78+
//pairs[1][0] = 2;
79+
//pairs[1][1] = 1;
80+
//pairs[2] = malloc(2 * sizeof(int));
81+
//pairs[2][0] = 3;
82+
//pairs[2][1] = 2;
83+
//pairs[3] = malloc(2 * sizeof(int));
84+
//pairs[3][0] = 4;
85+
//pairs[3][1] = 0;
86+
//pairs[4] = malloc(2 * sizeof(int));
87+
//pairs[4][0] = 0;
88+
//pairs[4][1] = 5;
89+
90+
int count = 0;
91+
int *ids = findOrder(course_num, pairs, pair_num, 2, &count);
92+
for (i = 0; i < count; i++) {
93+
printf("%d ", ids[i]);
94+
}
95+
printf("\n");
96+
return 0;
97+
}

0 commit comments

Comments
 (0)