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