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 0facc38

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 5136935 commit 0facc38

File tree

5 files changed

+63
-63
lines changed

5 files changed

+63
-63
lines changed

‎091_decode_ways/decode_ways.c‎

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

5-
static int numDecodings(char* s) {
5+
6+
static int numDecodings(char* s)
7+
{
68
int len = strlen(s);
79
if (len == 0) {
810
return 0;
911
}
1012

11-
int dp[len + 1];
12-
memset(dp, 0, (len + 1) * sizeof(int));
13-
14-
dp[0] = 1;
15-
dp[1] = s[0] == '0' ? 0 : 1;
13+
int a = 1;
14+
int b = s[0] == '0' ? 0 : a;
15+
int c = b;
16+
/* DP: How many counts in sequence c = f(a, b) and c counts s[i - 1] */
1617
for (int i = 2; i <= len; i++) {
17-
if (s[i - 1] != '0') {
18-
dp[i] = dp[i - 1];
19-
}
20-
18+
c = s[i - 1] == '0' ? 0 : b;
2119
int num = (s[i - 2] - '0') * 10 + (s[i - 1] - '0');
2220
if (num >= 10 && num <= 26) {
23-
dp[i] += dp[i-2];
21+
c += a;
2422
}
23+
a = b;
24+
b = c;
2525
}
2626

27-
return dp[len];
27+
return c;
2828
}
2929

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

‎120_triangle/triangle.c‎

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

6+
67
static int dfs(int** triangle, int row_size, int *col_sizes,
78
int row, int col, int **sums, bool **passes)
89
{
@@ -14,24 +15,23 @@ static int dfs(int** triangle, int row_size, int *col_sizes,
1415
int s1 = dfs(triangle, row_size, col_sizes, row + 1, col, sums, passes);
1516
int s2 = dfs(triangle, row_size, col_sizes, row + 1, col + 1, sums, passes);
1617
sums[row][col] = triangle[row][col] + (s1 < s2 ? s1 : s2);
18+
/* Set pass marks in backtracing as the paths are overlapped */
1719
passes[row][col] = true;
1820
return sums[row][col];
1921
}
2022
}
2123

22-
static int minimumTotal(int** triangle, int triangleRowSize, int *triangleColSizes)
24+
static int minimumTotal(int** triangle, int triangleSize, int *triangleColSizes)
2325
{
2426
int i;
25-
bool **passes = malloc(triangleRowSize * sizeof(bool *));
26-
for (i = 0; i < triangleRowSize; i++) {
27+
int **sums = malloc(triangleSize * sizeof(int *));
28+
bool **passes = malloc(triangleSize * sizeof(bool *));
29+
for (i = 0; i < triangleSize; i++) {
2730
passes[i] = malloc(triangleColSizes[i]);
2831
memset(passes[i], false, triangleColSizes[i]);
29-
}
30-
int **sums = malloc(triangleRowSize * sizeof(int *));
31-
for (i = 0; i < triangleRowSize; i++) {
3232
sums[i] = malloc(triangleColSizes[i] * sizeof(int));
3333
}
34-
return dfs(triangle, triangleRowSize, triangleColSizes, 0, 0, sums, passes);
34+
return dfs(triangle, triangleSize, triangleColSizes, 0, 0, sums, passes);
3535
}
3636

3737
int main(void)

‎207_course_schedule/course_schedule.c‎

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

6+
67
struct graph_node {
78
int req_num;
89
int reqs[15];
10+
bool touched;
11+
bool taken;
912
};
1013

11-
static bool dfs(struct graph_node *courses, int id, bool*takens, bool*touched)
14+
static bool dfs(struct graph_node *courses, int id)
1215
{
1316
int i;
14-
if (touched[id]) {
17+
if (courses[id].touched) {
1518
return true;
16-
} else if (takens[id]) {
19+
} else if (courses[id].taken) {
1720
return false;
1821
} else {
19-
takens[id] = true;
22+
courses[id].taken = true;
2023
for (i = 0; i < courses[id].req_num; i++) {
21-
if (!dfs(courses, courses[id].reqs[i], takens, touched)) {
24+
if (!dfs(courses, courses[id].reqs[i])) {
2225
return false;
2326
}
2427
}
25-
/* marked as available and no need to traverse next time */
26-
touched[id] = true;
27-
takens[id] = false;
28+
/* If paths overlapped, mark in backtracing for no need to traverse next time */
29+
courses[id].touched = true;
30+
courses[id].taken = false;
2831
return true;
2932
}
3033
}
3134

32-
static bool canFinish(int numCourses, int** prerequisites, int prerequisitesRowSize, int prerequisitesColSize)
35+
static bool canFinish(int numCourses, int** prerequisites, int prerequisitesSize, int *prerequisitesColSize)
3336
{
3437
int i;
35-
bool *takens = malloc(numCourses);
36-
bool *touched = malloc(numCourses);
3738
struct graph_node *courses = malloc(numCourses * sizeof(*courses));
3839

3940
memset(courses, 0, numCourses * sizeof(*courses));
40-
memset(takens, false, numCourses * sizeof(bool));
41-
memset(touched, false, numCourses * sizeof(bool));
42-
43-
for (i = 0; i < prerequisitesRowSize; i++) {
41+
for (i = 0; i < prerequisitesSize; i++) {
4442
int id = prerequisites[i][0];
4543
int req = prerequisites[i][1];
4644
courses[id].reqs[courses[id].req_num++] = req;
4745
}
4846

4947
for (i = 0; i < numCourses; i++) {
50-
if (!dfs(courses, i, takens, touched)) {
48+
if (!dfs(courses, i)) {
5149
return false;
5250
}
5351
}
@@ -59,24 +57,31 @@ int main(void)
5957
{
6058
int i, course_num = 6, pair_num = 6;
6159
int **pairs = malloc(pair_num * sizeof(int *));
60+
int *col_sizes = malloc(pair_num * sizeof(int));
6261
pairs[0] = malloc(2 * sizeof(int));
6362
pairs[0][0] = 1;
6463
pairs[0][1] = 0;
64+
col_sizes[0] = 2;
6565
pairs[1] = malloc(2 * sizeof(int));
6666
pairs[1][0] = 2;
6767
pairs[1][1] = 1;
68+
col_sizes[1] = 2;
6869
pairs[2] = malloc(2 * sizeof(int));
6970
pairs[2][0] = 3;
7071
pairs[2][1] = 2;
72+
col_sizes[2] = 2;
7173
pairs[3] = malloc(2 * sizeof(int));
7274
pairs[3][0] = 1;
7375
pairs[3][1] = 3;
76+
col_sizes[3] = 2;
7477
pairs[4] = malloc(2 * sizeof(int));
7578
pairs[4][0] = 4;
7679
pairs[4][1] = 0;
80+
col_sizes[4] = 2;
7781
pairs[5] = malloc(2 * sizeof(int));
7882
pairs[5][0] = 0;
7983
pairs[5][1] = 5;
80-
printf("%s\n", canFinish(course_num, pairs, pair_num, 2) ? "true" : "false");
84+
col_sizes[5] = 2;
85+
printf("%s\n", canFinish(course_num, pairs, pair_num, col_sizes) ? "true" : "false");
8186
return 0;
8287
}

‎210_course_schedule_ii/course_schedule.c‎

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

6+
67
struct graph_node {
78
int req_num;
89
int reqs[15];
10+
bool touched;
11+
bool taken;
912
};
1013

11-
static bool dfs(struct graph_node *courses, int id, bool*takens, bool*touched, int *order, int *count)
14+
static bool dfs(struct graph_node *courses, int id, int *order, int *count)
1215
{
1316
int i;
14-
if (touched[id]) {
17+
if (courses[id].touched) {
1518
return true;
16-
} else if (takens[id]) {
19+
} else if (courses[id].taken) {
1720
return false;
1821
} else {
19-
takens[id] = true;
22+
courses[id].taken = true;
2023
for (i = 0; i < courses[id].req_num; i++) {
21-
if (!dfs(courses, courses[id].reqs[i], takens, touched, order, count)) {
24+
if (!dfs(courses, courses[id].reqs[i], order, count)) {
2225
return false;
2326
}
2427
}
25-
/* marked as available and no need to traverse next time */
28+
/* Record path in backtrace before DFS return */
2629
order[(*count)++] = id;
27-
touched[id] = true;
28-
takens[id] = false;
30+
/* If paths overlapped, mark in backtracing for no need to traverse next time */
31+
courses[id].touched = true;
32+
courses[id].taken = false;
2933
return true;
3034
}
3135
}
@@ -34,27 +38,22 @@ static bool dfs(struct graph_node *courses, int id, bool *takens, bool *touched,
3438
* Return an array of size *returnSize.
3539
* Note: The returned array must be malloced, assume caller calls free().
3640
*/
37-
static int *findOrder(int numCourses, int** prerequisites, int prerequisitesRowSize, int prerequisitesColSize, int *returnSize)
41+
static int *findOrder(int numCourses, int** prerequisites, int prerequisitesSize, int *prerequisitesColSize, int *returnSize)
3842
{
3943
int i;
4044
int *order = malloc(numCourses * sizeof(int));
41-
bool *takens = malloc(numCourses);
42-
bool *touched = malloc(numCourses);
4345
struct graph_node *courses = malloc(numCourses * sizeof(*courses));
4446

4547
memset(courses, 0, numCourses * sizeof(*courses));
46-
memset(takens, false, numCourses * sizeof(bool));
47-
memset(touched, false, numCourses * sizeof(bool));
48-
49-
for (i = 0; i < prerequisitesRowSize; i++) {
48+
for (i = 0; i < prerequisitesSize; i++) {
5049
int id = prerequisites[i][0];
5150
int req = prerequisites[i][1];
5251
courses[id].reqs[courses[id].req_num++] = req;
5352
}
5453

5554
*returnSize = 0;
5655
for (i = 0; i < numCourses; i++) {
57-
if (!dfs(courses, i, takens, touched, order, returnSize)) {
56+
if (!dfs(courses, i, order, returnSize)) {
5857
*returnSize = 0;
5958
return order;
6059
}
@@ -67,6 +66,7 @@ int main(void)
6766
{
6867
int i, course_num = 3, pair_num = 1;
6968
int **pairs = malloc(pair_num * sizeof(int *));
69+
int *col_sizes = malloc(pair_num * sizeof(int));
7070
pairs[0] = malloc(2 * sizeof(int));
7171
pairs[0][0] = 1;
7272
pairs[0][1] = 0;
@@ -84,7 +84,7 @@ int main(void)
8484
//pairs[4][1] = 5;
8585

8686
int count = 0;
87-
int *ids = findOrder(course_num, pairs, pair_num, 2, &count);
87+
int *ids = findOrder(course_num, pairs, pair_num, col_sizes, &count);
8888
for (i = 0; i < count; i++) {
8989
printf("%d ", ids[i]);
9090
}

‎236_lowest_common_ancestor_of_a_binary_tree/bst_lca.c‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdlib.h>
33
#include <stdbool.h>
44

5+
56
struct TreeNode {
67
int val;
78
struct TreeNode *left;
@@ -11,24 +12,18 @@ struct TreeNode {
1112
static struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q)
1213
{
1314
if (root == NULL || root == p || root == q) {
15+
/* edge cases: if return NULL then no p or q node in this path */
1416
return root;
1517
}
1618

19+
/* l is the LCA in the left branch but not root->left */
1720
struct TreeNode *l = lowestCommonAncestor(root->left, p, q);
18-
if (l != NULL && l != p && l != q) {
19-
/* both p and q in left subtree: l->left != NULL && l->right != NULL */
20-
return l;
21-
}
22-
21+
/* r is the LCA in the right branch but not root->right */
2322
struct TreeNode *r = lowestCommonAncestor(root->right, p, q);
24-
if (r != NULL && r != p && r != q) {
25-
/* both p and q in right subtree: r->left != NULL && r->right != NULL */
26-
return r;
27-
}
28-
2923
if (l != NULL && r != NULL) {
3024
return root;
3125
} else {
26+
/* if not return root node, the return value is fixed */
3227
return l != NULL ? l : r;
3328
}
3429
}

0 commit comments

Comments
(0)

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