This commit is contained in:
krahets 2023-09-13 03:40:35 +08:00
parent 2f051b6ff8
commit 821898534f
6 changed files with 28 additions and 18 deletions

View File

@ -1947,6 +1947,10 @@ comments: true
/* 中间插入元素 */ /* 中间插入元素 */
void insert(myList *list, int index, int num) { void insert(myList *list, int index, int num) {
assert(index >= 0 && index < size(list)); assert(index >= 0 && index < size(list));
// 元素数量超出容量时,触发扩容机制
if (size(list) == capacity(list)) {
extendCapacity(list); // 扩容
}
for (int i = size(list); i > index; --i) { for (int i = size(list); i > index; --i) {
list->nums[i] = list->nums[i - 1]; list->nums[i] = list->nums[i - 1];
} }

View File

@ -162,7 +162,7 @@ status: new
int forLoop(int n) { int forLoop(int n) {
int res = 0; int res = 0;
// 循环求和 1, 2, ..., n-1, n // 循环求和 1, 2, ..., n-1, n
for (int i = 1; i <= n; ++i) { for (int i = 1; i <= n; i++) {
res += i; res += i;
} }
return res; return res;
@ -437,9 +437,9 @@ status: new
// 循环求和 1, 2, 4, 5... // 循环求和 1, 2, 4, 5...
while (i <= n) { while (i <= n) {
res += i; res += i;
i += 1; // 更新条件变量 // 更新条件变量
res += i; i += 1;
i *= 2; // 更新条件变量 i *= 2;
} }
return res; return res;
} }
@ -759,11 +759,10 @@ status: new
// n * n 为对应点数量,"(i, j), " 对应字符串长最大为 6+10*2加上最后一个空字符 \0 的额外空间 // n * n 为对应点数量,"(i, j), " 对应字符串长最大为 6+10*2加上最后一个空字符 \0 的额外空间
int size = n * n * 26 + 1; int size = n * n * 26 + 1;
char *res = malloc(size * sizeof(char)); char *res = malloc(size * sizeof(char));
// 循环 i = 1, 2, ..., n-1, n // 循环 i = 1, 2, ..., n-1, n
for (int i = 1; i <= n; ++i) { for (int i = 1; i <= n; i++) {
// 循环 j = 1, 2, ..., n-1, n // 循环 j = 1, 2, ..., n-1, n
for (int j = 1; j <= n; ++j) { for (int j = 1; j <= n; j++) {
char tmp[26]; char tmp[26];
snprintf(tmp, sizeof(tmp), "(%d, %d), ", i, j); snprintf(tmp, sizeof(tmp), "(%d, %d), ", i, j);
strncat(res, tmp, size - strlen(res) - 1); strncat(res, tmp, size - strlen(res) - 1);

View File

@ -31,7 +31,7 @@ comments: true
- 数据结构是算法的基石。数据结构为算法提供了结构化存储的数据,以及用于操作数据的方法。 - 数据结构是算法的基石。数据结构为算法提供了结构化存储的数据,以及用于操作数据的方法。
- 算法是数据结构发挥作用的舞台。数据结构本身仅存储数据信息,结合算法才能解决特定问题。 - 算法是数据结构发挥作用的舞台。数据结构本身仅存储数据信息,结合算法才能解决特定问题。
- 算法通常可以基于不同的数据结构进行实现,并往往有对应最优的数据结构,最终执行效率可能相差很大。 - 算法通常可以基于不同的数据结构进行实现,但执行效率可能相差很大,选择合适的数据结构是关键
![数据结构与算法的关系](what_is_dsa.assets/relationship_between_data_structure_and_algorithm.png) ![数据结构与算法的关系](what_is_dsa.assets/relationship_between_data_structure_and_algorithm.png)

View File

@ -137,7 +137,14 @@ comments: true
=== "Rust" === "Rust"
```rust title="" ```rust title=""
/* 标题注释,用于标注函数、类、测试样例等 */
// 内容注释,用于详解代码
/**
* 多行
* 注释
*/
``` ```
=== "C" === "C"

View File

@ -1660,7 +1660,7 @@ comments: true
} }
/* 判断队列是否为空 */ /* 判断队列是否为空 */
empty() { isEmpty() {
return this.#queSize === 0; return this.#queSize === 0;
} }
@ -1689,7 +1689,7 @@ comments: true
/* 访问队首元素 */ /* 访问队首元素 */
peek() { peek() {
if (this.empty()) throw new Error('队列为空'); if (this.isEmpty()) throw new Error('队列为空');
return this.#nums[this.#front]; return this.#nums[this.#front];
} }
@ -1730,7 +1730,7 @@ comments: true
} }
/* 判断队列是否为空 */ /* 判断队列是否为空 */
empty(): boolean { isEmpty(): boolean {
return this.queSize === 0; return this.queSize === 0;
} }
@ -1759,7 +1759,7 @@ comments: true
/* 访问队首元素 */ /* 访问队首元素 */
peek(): number { peek(): number {
if (this.empty()) throw new Error('队列为空'); if (this.isEmpty()) throw new Error('队列为空');
return this.nums[this.front]; return this.nums[this.front];
} }

View File

@ -1386,7 +1386,7 @@ comments: true
} }
/* 判断栈是否为空 */ /* 判断栈是否为空 */
empty() { isEmpty() {
return this.#stack.length === 0; return this.#stack.length === 0;
} }
@ -1397,13 +1397,13 @@ comments: true
/* 出栈 */ /* 出栈 */
pop() { pop() {
if (this.empty()) throw new Error('栈为空'); if (this.isEmpty()) throw new Error('栈为空');
return this.#stack.pop(); return this.#stack.pop();
} }
/* 访问栈顶元素 */ /* 访问栈顶元素 */
top() { top() {
if (this.empty()) throw new Error('栈为空'); if (this.isEmpty()) throw new Error('栈为空');
return this.#stack[this.#stack.length - 1]; return this.#stack[this.#stack.length - 1];
} }
@ -1430,7 +1430,7 @@ comments: true
} }
/* 判断栈是否为空 */ /* 判断栈是否为空 */
empty(): boolean { isEmpty(): boolean {
return this.stack.length === 0; return this.stack.length === 0;
} }
@ -1441,13 +1441,13 @@ comments: true
/* 出栈 */ /* 出栈 */
pop(): number | undefined { pop(): number | undefined {
if (this.empty()) throw new Error('栈为空'); if (this.isEmpty()) throw new Error('栈为空');
return this.stack.pop(); return this.stack.pop();
} }
/* 访问栈顶元素 */ /* 访问栈顶元素 */
top(): number | undefined { top(): number | undefined {
if (this.empty()) throw new Error('栈为空'); if (this.isEmpty()) throw new Error('栈为空');
return this.stack[this.stack.length - 1]; return this.stack[this.stack.length - 1];
} }