mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	Several bug fixes
This commit is contained in:
		@ -14,13 +14,13 @@ struct myList {
 | 
			
		||||
    int extendRatio; // 列表每次扩容的倍数
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct myList MyList;
 | 
			
		||||
typedef struct myList myList;
 | 
			
		||||
 | 
			
		||||
void extendCapacity(MyList *nums);
 | 
			
		||||
void extendCapacity(myList *nums);
 | 
			
		||||
 | 
			
		||||
/* 构造函数 */
 | 
			
		||||
MyList *newMyList() {
 | 
			
		||||
    MyList *nums = malloc(sizeof(MyList));
 | 
			
		||||
myList *newMyList() {
 | 
			
		||||
    myList *nums = malloc(sizeof(myList));
 | 
			
		||||
    nums->capacity = 10;
 | 
			
		||||
    nums->arr = malloc(sizeof(int) * nums->capacity);
 | 
			
		||||
    nums->size = 0;
 | 
			
		||||
@ -29,35 +29,35 @@ MyList *newMyList() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 析构函数 */
 | 
			
		||||
void delMyList(MyList *nums) {
 | 
			
		||||
void delMyList(myList *nums) {
 | 
			
		||||
    free(nums->arr);
 | 
			
		||||
    free(nums);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 获取列表长度 */
 | 
			
		||||
int size(MyList *nums) {
 | 
			
		||||
int size(myList *nums) {
 | 
			
		||||
    return nums->size;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 获取列表容量 */
 | 
			
		||||
int capacity(MyList *nums) {
 | 
			
		||||
int capacity(myList *nums) {
 | 
			
		||||
    return nums->capacity;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 访问元素 */
 | 
			
		||||
int get(MyList *nums, int index) {
 | 
			
		||||
int get(myList *nums, int index) {
 | 
			
		||||
    assert(index >= 0 && index < nums->size);
 | 
			
		||||
    return nums->arr[index];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 更新元素 */
 | 
			
		||||
void set(MyList *nums, int index, int num) {
 | 
			
		||||
void set(myList *nums, int index, int num) {
 | 
			
		||||
    assert(index >= 0 && index < nums->size);
 | 
			
		||||
    nums->arr[index] = num;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 尾部添加元素 */
 | 
			
		||||
void add(MyList *nums, int num) {
 | 
			
		||||
void add(myList *nums, int num) {
 | 
			
		||||
    if (size(nums) == capacity(nums)) {
 | 
			
		||||
        extendCapacity(nums); // 扩容
 | 
			
		||||
    }
 | 
			
		||||
@ -66,7 +66,7 @@ void add(MyList *nums, int num) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 中间插入元素 */
 | 
			
		||||
void insert(MyList *nums, int index, int num) {
 | 
			
		||||
void insert(myList *nums, int index, int num) {
 | 
			
		||||
    assert(index >= 0 && index < size(nums));
 | 
			
		||||
    // 元素数量超出容量时,触发扩容机制
 | 
			
		||||
    if (size(nums) == capacity(nums)) {
 | 
			
		||||
@ -81,7 +81,7 @@ void insert(MyList *nums, int index, int num) {
 | 
			
		||||
 | 
			
		||||
/* 删除元素 */
 | 
			
		||||
// 注意:stdio.h 占用了 remove 关键词
 | 
			
		||||
int removeNum(MyList *nums, int index) {
 | 
			
		||||
int removeNum(myList *nums, int index) {
 | 
			
		||||
    assert(index >= 0 && index < size(nums));
 | 
			
		||||
    int num = nums->arr[index];
 | 
			
		||||
    for (int i = index; i < size(nums) - 1; i++) {
 | 
			
		||||
@ -92,7 +92,7 @@ int removeNum(MyList *nums, int index) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 列表扩容 */
 | 
			
		||||
void extendCapacity(MyList *nums) {
 | 
			
		||||
void extendCapacity(myList *nums) {
 | 
			
		||||
    // 先分配空间
 | 
			
		||||
    int newCapacity = capacity(nums) * nums->extendRatio;
 | 
			
		||||
    int *extend = (int *)malloc(sizeof(int) * newCapacity);
 | 
			
		||||
@ -111,14 +111,14 @@ void extendCapacity(MyList *nums) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 将列表转换为 Array 用于打印 */
 | 
			
		||||
int *toArray(MyList *nums) {
 | 
			
		||||
int *toArray(myList *nums) {
 | 
			
		||||
    return nums->arr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
int main() {
 | 
			
		||||
    /* 初始化列表 */
 | 
			
		||||
    MyList *nums = newMyList();
 | 
			
		||||
    myList *nums = newMyList();
 | 
			
		||||
    /* 尾部添加元素 */
 | 
			
		||||
    add(nums, 1);
 | 
			
		||||
    add(nums, 3);
 | 
			
		||||
 | 
			
		||||
@ -9,6 +9,7 @@
 | 
			
		||||
#define MAX_N 100
 | 
			
		||||
#define MAX_RES 1000
 | 
			
		||||
 | 
			
		||||
/* 放置结果 */
 | 
			
		||||
struct result {
 | 
			
		||||
    char ***data;
 | 
			
		||||
    int size;
 | 
			
		||||
@ -17,8 +18,8 @@ struct result {
 | 
			
		||||
typedef struct result Result;
 | 
			
		||||
 | 
			
		||||
/* 回溯算法:N 皇后 */
 | 
			
		||||
void backtrack(int row, int n, char state[MAX_N][MAX_N], Result *res, 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_N][MAX_N], Result *res, 
 | 
			
		||||
        bool cols[MAX_N], bool diags1[2 * MAX_N - 1], bool diags2[2 * MAX_N - 1]) {
 | 
			
		||||
    // 当放置完所有行时,记录解
 | 
			
		||||
    if (row == n) {
 | 
			
		||||
        res->data[res->size] = (char **)malloc(sizeof(char *) * n);
 | 
			
		||||
 | 
			
		||||
@ -123,6 +123,8 @@
 | 
			
		||||
=== "C"
 | 
			
		||||
 | 
			
		||||
    ```c title="n_queens.c"
 | 
			
		||||
    [class]{result}-[func]{}
 | 
			
		||||
 | 
			
		||||
    [class]{}-[func]{backtrack}
 | 
			
		||||
 | 
			
		||||
    [class]{}-[func]{nQueens}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user