Skip to content

Commit

Permalink
Merge branch 'krahets:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonglja authored Oct 28, 2023
2 parents 10bad07 + c37f098 commit 54b3a22
Show file tree
Hide file tree
Showing 575 changed files with 1,645 additions and 1,152 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This workflow will install Dart SDK, run format, analyze and build with Dart

name: Dart

on:
push:
branches: ["main"]
paths: ["codes/dart/**/*.dart"]
pull_request:
branches: ["main"]
paths: ["codes/dart/**/*.dart"]
workflow_dispatch:

permissions:
contents: read

jobs:
build:
name: Dart ${{ matrix.dart-sdk }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
dart-sdk: [stable]
steps:
- uses: actions/checkout@v4
- name: Set up Dart ${{ matrix.dart-sdk }}
uses: dart-lang/setup-dart@v1
with:
sdk: ${{ matrix.dart-sdk}}
- name: Run format
run: dart format codes/dart
- name: Run analyze
run: dart analyze codes/dart
- name: Run build
run: dart codes/dart/build.dart
39 changes: 39 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET

on:
push:
branches: [ "main" ]
paths: ["codes/csharp/**/*.cs"]
pull_request:
branches: [ "main" ]
paths: ["codes/csharp/**/*.cs"]
workflow_dispatch:

jobs:
build:
name: .NET ${{ matrix.dotnet-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
defaults:
run:
working-directory: codes/csharp/
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
dotnet-version: ["6.0.x"]

steps:
- uses: actions/checkout@v4

- name: Setup .NET ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ matrix.dotnet-version }}
- name: Restore dependencies
run: dotnet restore hello-algo.csproj
- name: Build
run: dotnet build --no-restore hello-algo.csproj
- name: Test with dotnet
run: dotnet test hello-algo.csproj
5 changes: 3 additions & 2 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
pull_request:
branches: ["main"]
paths: ["codes/python/**/*.py"]
workflow_dispatch:

permissions:
contents: read
Expand All @@ -23,9 +24,9 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.8", "3.10"]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
pull_request:
branches: ["main"]
paths: ["codes/swift/**/*.swift"]
workflow_dispatch:

jobs:
build:
Expand Down
4 changes: 3 additions & 1 deletion codes/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ include_directories(./include)
add_subdirectory(chapter_computational_complexity)
add_subdirectory(chapter_array_and_linkedlist)
add_subdirectory(chapter_stack_and_queue)
add_subdirectory(chapter_heap)
add_subdirectory(chapter_hashing)
add_subdirectory(chapter_tree)
add_subdirectory(chapter_heap)
add_subdirectory(chapter_graph)
add_subdirectory(chapter_searching)
add_subdirectory(chapter_sorting)
add_subdirectory(chapter_divide_and_conquer)
add_subdirectory(chapter_backtracking)
add_subdirectory(chapter_dynamic_programming)
add_subdirectory(chapter_greedy)
17 changes: 8 additions & 9 deletions codes/c/chapter_backtracking/n_queens.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

#include "../utils/common.h"

#define MAX_N 100
#define MAX_RES 1000
#define MAX_SIZE 100

/* 回溯算法:N 皇后 */
void backtrack(int row, int n, char state[MAX_N][MAX_N], char ***res, int *resSize, bool cols[MAX_N],
bool diags1[2 * MAX_N - 1], bool diags2[2 * MAX_N - 1]) {
void backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],
bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {
// 当放置完所有行时,记录解
if (row == n) {
res[*resSize] = (char **)malloc(sizeof(char *) * n);
Expand Down Expand Up @@ -43,19 +42,19 @@ void backtrack(int row, int n, char state[MAX_N][MAX_N], char ***res, int *resSi

/* 求解 N 皇后 */
char ***nQueens(int n, int *returnSize) {
char state[MAX_N][MAX_N];
char state[MAX_SIZE][MAX_SIZE];
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
state[i][j] = '#';
}
state[i][n] = '\0';
}
bool cols[MAX_N] = {false}; // 记录列是否有皇后
bool diags1[2 * MAX_N - 1] = {false}; // 记录主对角线是否有皇后
bool diags2[2 * MAX_N - 1] = {false}; // 记录副对角线是否有皇后
bool cols[MAX_SIZE] = {false}; // 记录列是否有皇后
bool diags1[2 * MAX_SIZE - 1] = {false}; // 记录主对角线是否有皇后
bool diags2[2 * MAX_SIZE - 1] = {false}; // 记录副对角线是否有皇后

char ***res = (char ***)malloc(sizeof(char **) * MAX_RES);
char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);
*returnSize = 0;
backtrack(0, n, state, res, returnSize, cols, diags1, diags2);
return res;
Expand Down
34 changes: 19 additions & 15 deletions codes/c/chapter_backtracking/preorder_traversal_i_compact.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@

#include "../utils/common.h"

vector *res;
// 假设结果长度不超过 100
#define MAX_SIZE 100

// 打印向量中的元素
void printFunc(vector *v, void *p) {
TreeNode *node = p;
printf("%d ", node->val);
}
TreeNode *res[MAX_SIZE];
int resSize = 0;

/* 前序遍历:例题一 */
void preOrder(TreeNode *root) {
static void preOrder(TreeNode *root) {
if (root == NULL) {
return;
}
if (root->val == 7) {
// 记录解
vectorPushback(res, root, sizeof(int));
res[resSize++] = root;
}
preOrder(root->left);
preOrder(root->right);
Expand All @@ -30,15 +28,21 @@ void preOrder(TreeNode *root) {
/* Driver Code */
int main() {
int arr[] = {1, 7, 3, 4, 5, 6, 7};
res = newVector();
TreeNode *root = arrToTree(arr, sizeof(arr) / sizeof(arr[0]));
printf("\n初始化二叉树\r\n");
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
printf("\n初始化二叉树\n");
printTree(root);

// 前序遍历
preOrder(root);

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

// 释放内存
freeMemoryTree(root);
return 0;
}
67 changes: 30 additions & 37 deletions codes/c/chapter_backtracking/preorder_traversal_ii_compact.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,55 @@

#include "../utils/common.h"

// 假设路径和结果长度不超过 100
#define MAX_SIZE 100
#define MAX_RES_SIZE 100

TreeNode *path[MAX_SIZE];
TreeNode *res[MAX_RES_SIZE][MAX_SIZE];
int pathSize = 0, resSize = 0;

/* 前序遍历:例题二 */
void preOrder(TreeNode *root, vector *path, vector *res) {
static void preOrder(TreeNode *root) {
if (root == NULL) {
return;
}
// 尝试
vectorPushback(path, root, sizeof(TreeNode));
path[pathSize++] = root;
if (root->val == 7) {
// 记录解
vector *newPath = newVector();
for (int i = 0; i < path->size; i++) {
vectorPushback(newPath, path->data[i], sizeof(int));
for (int i = 0; i < pathSize; ++i) {
res[resSize][i] = path[i];
}
vectorPushback(res, newPath, sizeof(vector));
resSize++;
}

preOrder(root->left, path, res);
preOrder(root->right, path, res);

preOrder(root->left);
preOrder(root->right);
// 回退
vectorPopback(path);
}

// 打印向量中的元素
void printResult(vector *vv) {
for (int i = 0; i < vv->size; i++) {
vector *v = (vector *)vv->data[i];
for (int j = 0; j < v->size; j++) {
TreeNode *node = (TreeNode *)v->data[j];
printf("%d ", node->val);
}
printf("\n");
}
pathSize--;
}

/* Driver Code */
int main() {
int arr[] = {1, 7, 3, 4, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
TreeNode *root = arrToTree(arr, n);
printf("\r\n初始化二叉树\r\n");
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
printf("\n初始化二叉树\n");
printTree(root);

// 创建存储路径和结果的向量
vector *path = newVector();
vector *res = newVector();

// 前序遍历
preOrder(root, path, res);

// 输出结果
printf("输出所有根节点到节点 7 的路径:\n");
printResult(res);
preOrder(root);

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

// 释放内存
delVector(path);
delVector(res);
freeMemoryTree(root);
return 0;
}
Loading

0 comments on commit 54b3a22

Please sign in to comment.