Skip to content

Commit

Permalink
fix: Update C code for compatibility with the MSVC compiler (krahets#949
Browse files Browse the repository at this point in the history
)

* Replace VLA with malloc
Replace VLA with malloc to make C code
compatible with cl compiler on Windows.

* Fix C code for CI compiler.

* Fix C code compability to CI.

* check the trigger
  • Loading branch information
krahets authored Nov 16, 2023
1 parent e4aa76e commit f7c41b6
Show file tree
Hide file tree
Showing 23 changed files with 233 additions and 172 deletions.
2 changes: 1 addition & 1 deletion codes/c/chapter_array_and_linkedlist/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int main() {
printf("数组 arr = ");
printArray(arr, size);

int nums[5] = {1, 3, 2, 5, 4};
int nums[] = {1, 3, 2, 5, 4};
printf("数组 nums = ");
printArray(nums, size);

Expand Down
3 changes: 2 additions & 1 deletion codes/c/chapter_backtracking/preorder_traversal_i_compact.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ int main() {
preOrder(root);

printf("\n输出所有值为 7 的节点\n");
int vals[resSize];
int *vals = malloc(resSize * sizeof(int));
for (int i = 0; i < resSize; i++) {
vals[i] = res[i]->val;
}
printArray(vals, resSize);

// 释放内存
freeMemoryTree(root);
free(vals);
return 0;
}
3 changes: 2 additions & 1 deletion codes/c/chapter_backtracking/preorder_traversal_ii_compact.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ int main() {

printf("\n输出所有根节点到节点 7 的路径\n");
for (int i = 0; i < resSize; ++i) {
int vals[MAX_SIZE];
int *vals = malloc(MAX_SIZE * sizeof(int));
int size = 0;
for (int j = 0; res[i][j] != NULL; ++j) {
vals[size++] = res[i][j]->val;
}
printArray(vals, size);
free(vals);
}

// 释放内存
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ int main() {

printf("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点\n");
for (int i = 0; i < resSize; ++i) {
int vals[MAX_SIZE];
int *vals = malloc(MAX_SIZE * sizeof(int));
int size = 0;
for (int j = 0; res[i][j] != NULL; ++j) {
vals[size++] = res[i][j]->val;
}
printArray(vals, size);
free(vals);
}

// 释放内存
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ int main() {

printf("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点\n");
for (int i = 0; i < resSize; ++i) {
int vals[MAX_SIZE];
int *vals = malloc(MAX_SIZE * sizeof(int));
int size = 0;
for (int j = 0; res[i][j] != NULL; ++j) {
vals[size++] = res[i][j]->val;
}
printArray(vals, size);
free(vals);
}

// 释放内存
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ int climbingStairsConstraintDP(int n) {
return 1;
}
// 初始化 dp 表,用于存储子问题的解
int dp[n + 1][3];
memset(dp, 0, sizeof(dp));
int **dp = malloc((n + 1) * sizeof(int *));
for (int i = 0; i <= n; i++) {
dp[i] = calloc(3, sizeof(int));
}
// 初始状态:预设最小子问题的解
dp[1][1] = 1;
dp[1][2] = 0;
Expand All @@ -24,7 +26,13 @@ int climbingStairsConstraintDP(int n) {
dp[i][1] = dp[i - 1][2];
dp[i][2] = dp[i - 2][1] + dp[i - 2][2];
}
return dp[n][1] + dp[n][2];
int res = dp[n][1] + dp[n][2];
// 释放内存
for (int i = 0; i <= n; i++) {
free(dp[i]);
}
free(dp);
return res;
}

/* Driver Code */
Expand Down
28 changes: 19 additions & 9 deletions codes/c/chapter_dynamic_programming/coin_change.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../utils/common.h"

/* 求最小值 */
int min(int a, int b) {
int myMin(int a, int b) {
return a < b ? a : b;
}

Expand All @@ -16,8 +16,10 @@ int coinChangeDP(int coins[], int amt, int coinsSize) {
int n = coinsSize;
int MAX = amt + 1;
// 初始化 dp 表
int dp[n + 1][amt + 1];
memset(dp, 0, sizeof(dp));
int **dp = malloc((n + 1) * sizeof(int *));
for (int i = 0; i <= n; i++) {
dp[i] = calloc(amt + 1, sizeof(int));
}
// 状态转移:首行首列
for (int a = 1; a <= amt; a++) {
dp[0][a] = MAX;
Expand All @@ -30,20 +32,25 @@ int coinChangeDP(int coins[], int amt, int coinsSize) {
dp[i][a] = dp[i - 1][a];
} else {
// 不选和选硬币 i 这两种方案的较小值
dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);
dp[i][a] = myMin(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);
}
}
}
return dp[n][amt] != MAX ? dp[n][amt] : -1;
int res = dp[n][amt] != MAX ? dp[n][amt] : -1;
// 释放内存
for (int i = 0; i <= n; i++) {
free(dp[i]);
}
free(dp);
return res;
}

/* 零钱兑换:空间优化后的动态规划 */
int coinChangeDPComp(int coins[], int amt, int coinsSize) {
int n = coinsSize;
int MAX = amt + 1;
// 初始化 dp 表
int dp[amt + 1];
memset(dp, MAX, sizeof(dp));
int *dp = calloc(amt + 1, sizeof(int));
dp[0] = 0;
// 状态转移
for (int i = 1; i <= n; i++) {
Expand All @@ -53,11 +60,14 @@ int coinChangeDPComp(int coins[], int amt, int coinsSize) {
dp[a] = dp[a];
} else {
// 不选和选硬币 i 这两种方案的较小值
dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);
dp[a] = myMin(dp[a], dp[a - coins[i - 1]] + 1);
}
}
}
return dp[amt] != MAX ? dp[amt] : -1;
int res = dp[amt] != MAX ? dp[amt] : -1;
// 释放内存
free(dp);
return res;
}

/* Driver code */
Expand Down
22 changes: 16 additions & 6 deletions codes/c/chapter_dynamic_programming/coin_change_ii.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
int coinChangeIIDP(int coins[], int amt, int coinsSize) {
int n = coinsSize;
// 初始化 dp 表
int dp[n + 1][amt + 1];
memset(dp, 0, sizeof(dp));
int **dp = malloc((n + 1) * sizeof(int *));
for (int i = 0; i <= n; i++) {
dp[i] = calloc(amt + 1, sizeof(int));
}
// 初始化首列
for (int i = 0; i <= n; i++) {
dp[i][0] = 1;
Expand All @@ -28,15 +30,20 @@ int coinChangeIIDP(int coins[], int amt, int coinsSize) {
}
}
}
return dp[n][amt];
int res = dp[n][amt];
// 释放内存
for (int i = 0; i <= n; i++) {
free(dp[i]);
}
free(dp);
return res;
}

/* 零钱兑换 II:空间优化后的动态规划 */
int coinChangeIIDPComp(int coins[], int amt, int coinsSize) {
int n = coinsSize;
// 初始化 dp 表
int dp[amt + 1];
memset(dp, 0, sizeof(dp));
int *dp = calloc(amt + 1, sizeof(int));
dp[0] = 1;
// 状态转移
for (int i = 1; i <= n; i++) {
Expand All @@ -50,7 +57,10 @@ int coinChangeIIDPComp(int coins[], int amt, int coinsSize) {
}
}
}
return dp[amt];
int res = dp[amt];
// 释放内存
free(dp);
return res;
}

/* Driver code */
Expand Down
47 changes: 32 additions & 15 deletions codes/c/chapter_dynamic_programming/edit_distance.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#include "../utils/common.h"

/* 求最小值 */
int min(int a, int b) {
int myMin(int a, int b) {
return a < b ? a : b;
}

/* 编辑距离:暴力搜索 */
int editDistanceDFS(char *s, char *t, int i, int j) {
int editDistanceDFS(char *s, char *t, int i, int j) {
// 若 s 和 t 都为空,则返回 0
if (i == 0 && j == 0)
return 0;
Expand All @@ -30,11 +30,11 @@ int editDistanceDFS(char *s, char *t, int i, int j) {
int del = editDistanceDFS(s, t, i - 1, j);
int replace = editDistanceDFS(s, t, i - 1, j - 1);
// 返回最少编辑步数
return min(min(insert, del), replace) + 1;
return myMin(myMin(insert, del), replace) + 1;
}

/* 编辑距离:记忆化搜索 */
int editDistanceDFSMem(char *s, char *t, int memCols, int mem[][memCols], int i, int j) {
int editDistanceDFSMem(char *s, char *t, int memCols, int **mem, int i, int j) {
// 若 s 和 t 都为空,则返回 0
if (i == 0 && j == 0)
return 0;
Expand All @@ -55,14 +55,16 @@ int editDistanceDFSMem(char *s, char *t, int memCols, int mem[][memCols], int i,
int del = editDistanceDFSMem(s, t, memCols, mem, i - 1, j);
int replace = editDistanceDFSMem(s, t, memCols, mem, i - 1, j - 1);
// 记录并返回最少编辑步数
mem[i][j] = min(min(insert, del), replace) + 1;
mem[i][j] = myMin(myMin(insert, del), replace) + 1;
return mem[i][j];
}

/* 编辑距离:动态规划 */
int editDistanceDP(char *s, char *t, int n, int m) {
int dp[n + 1][m + 1];
memset(dp, 0, sizeof(dp));
int **dp = malloc((n + 1) * sizeof(int *));
for (int i = 0; i <= n; i++) {
dp[i] = calloc(m + 1, sizeof(int));
}
// 状态转移:首行首列
for (int i = 1; i <= n; i++) {
dp[i][0] = i;
Expand All @@ -78,17 +80,21 @@ int editDistanceDP(char *s, char *t, int n, int m) {
dp[i][j] = dp[i - 1][j - 1];
} else {
// 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1
dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
dp[i][j] = myMin(myMin(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
}
}
}
return dp[n][m];
int res = dp[n][m];
// 释放内存
for (int i = 0; i <= n; i++) {
free(dp[i]);
}
return res;
}

/* 编辑距离:空间优化后的动态规划 */
int editDistanceDPComp(char *s, char *t, int n, int m) {
int dp[m + 1];
memset(dp, 0, sizeof(dp));
int *dp = calloc(m + 1, sizeof(int));
// 状态转移:首行
for (int j = 1; j <= m; j++) {
dp[j] = j;
Expand All @@ -106,12 +112,15 @@ int editDistanceDPComp(char *s, char *t, int n, int m) {
dp[j] = leftup;
} else {
// 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1
dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;
dp[j] = myMin(myMin(dp[j - 1], dp[j]), leftup) + 1;
}
leftup = temp; // 更新为下一轮的 dp[i-1, j-1]
}
}
return dp[m];
int res = dp[m];
// 释放内存
free(dp);
return res;
}

/* Driver Code */
Expand All @@ -125,10 +134,18 @@ int main() {
printf("将 %s 更改为 %s 最少需要编辑 %d 步\n", s, t, res);

// 记忆化搜索
int mem[n + 1][m + 1];
memset(mem, -1, sizeof(mem));
int **mem = malloc((n + 1) * sizeof(int *));
for (int i = 0; i <= n; i++) {
mem[i] = malloc((m + 1) * sizeof(int));
memset(mem[i], -1, (m + 1) * sizeof(int));
}
res = editDistanceDFSMem(s, t, m + 1, mem, n, m);
printf("将 %s 更改为 %s 最少需要编辑 %d 步\n", s, t, res);
// 释放内存
for (int i = 0; i <= n; i++) {
free(mem[i]);
}
free(mem);

// 动态规划
res = editDistanceDP(s, t, n, m);
Expand Down
Loading

0 comments on commit f7c41b6

Please sign in to comment.