This commit is contained in:
krahets
2023-02-08 04:17:26 +08:00
parent 7f4efa6d5e
commit 0407cc720c
347 changed files with 150 additions and 132904 deletions

View File

@@ -123,14 +123,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C++"
```cpp title="array.cpp"
/* 随机返回一个数组元素 */
int randomAccess(int* nums, int size) {
// 在区间 [0, size) 中随机抽取一个数字
int randomIndex = rand() % size;
// 获取并返回随机元素
int randomNum = nums[randomIndex];
return randomNum;
}
[class]{}-[func]{randomAccess}
```
=== "Python"
@@ -238,19 +231,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C++"
```cpp title="array.cpp"
/* 扩展数组长度 */
int* extend(int* nums, int size, int enlarge) {
// 初始化一个扩展长度后的数组
int* res = new int[size + enlarge];
// 将原数组中的所有元素复制到新数组
for (int i = 0; i < size; i++) {
res[i] = nums[i];
}
// 释放内存
delete[] nums;
// 返回扩展后的新数组
return res;
}
[class]{}-[func]{extend}
```
=== "Python"
@@ -383,23 +364,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C++"
```cpp title="array.cpp"
/* 在数组的索引 index 处插入元素 num */
void insert(int* nums, int size, int num, int index) {
// 把索引 index 以及之后的所有元素向后移动一位
for (int i = size - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
void remove(int* nums, int size, int index) {
// 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < size - 1; i++) {
nums[i] = nums[i + 1];
}
}
[class]{}-[func]{insert}
[class]{}-[func]{remove}
```
=== "Python"
@@ -567,14 +534,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C++"
```cpp title="array.cpp"
/* 遍历数组 */
void traverse(int* nums, int size) {
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < size; i++) {
count++;
}
}
[class]{}-[func]{traverse}
```
=== "Python"
@@ -707,14 +667,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C++"
```cpp title="array.cpp"
/* 在数组中查找指定元素 */
int find(int* nums, int size, int target) {
for (int i = 0; i < size; i++) {
if (nums[i] == target)
return i;
}
return -1;
}
[class]{}-[func]{find}
```
=== "Python"

View File

@@ -511,15 +511,7 @@ comments: true
=== "C++"
```cpp title="linked_list.cpp"
/* 访问链表中索引为 index 的结点 */
ListNode* access(ListNode* head, int index) {
for (int i = 0; i < index; i++) {
if (head == nullptr)
return nullptr;
head = head->next;
}
return head;
}
[class]{}-[func]{access}
```
=== "Python"
@@ -640,17 +632,7 @@ comments: true
=== "C++"
```cpp title="linked_list.cpp"
/* 在链表中查找值为 target 的首个结点 */
int find(ListNode* head, int target) {
int index = 0;
while (head != nullptr) {
if (head->val == target)
return index;
head = head->next;
index++;
}
return -1;
}
[class]{}-[func]{find}
```
=== "Python"

View File

@@ -722,106 +722,7 @@ comments: true
=== "C++"
```cpp title="my_list.cpp"
/* 列表类简易实现 */
class MyList {
private:
int* nums; // 数组(存储列表元素)
int numsCapacity = 10; // 列表容量
int numsSize = 0; // 列表长度(即当前元素数量)
int extendRatio = 2; // 每次列表扩容的倍数
public:
/* 构造函数 */
MyList() {
nums = new int[numsCapacity];
}
/* 析构函数 */
~MyList() {
delete[] nums;
}
/* 获取列表长度(即当前元素数量)*/
int size() {
return numsSize;
}
/* 获取列表容量 */
int capacity() {
return numsCapacity;
}
/* 访问元素 */
int get(int index) {
// 索引如果越界则抛出异常,下同
if (index < 0 || index >= size())
throw out_of_range("索引越界");
return nums[index];
}
/* 更新元素 */
void set(int index, int num) {
if (index < 0 || index >= size())
throw out_of_range("索引越界");
nums[index] = num;
}
/* 尾部添加元素 */
void add(int num) {
// 元素数量超出容量时,触发扩容机制
if (size() == capacity())
extendCapacity();
nums[size()] = num;
// 更新元素数量
numsSize++;
}
/* 中间插入元素 */
void insert(int index, int num) {
if (index < 0 || index >= size())
throw out_of_range("索引越界");
// 元素数量超出容量时,触发扩容机制
if (size() == capacity())
extendCapacity();
// 索引 i 以及之后的元素都向后移动一位
for (int j = size() - 1; j >= index; j--) {
nums[j + 1] = nums[j];
}
nums[index] = num;
// 更新元素数量
numsSize++;
}
/* 删除元素 */
int remove(int index) {
if (index < 0 || index >= size())
throw out_of_range("索引越界");
int num = nums[index];
// 索引 i 之后的元素都向前移动一位
for (int j = index; j < size() - 1; j++) {
nums[j] = nums[j + 1];
}
// 更新元素数量
numsSize--;
// 返回被删除元素
return num;
}
/* 列表扩容 */
void extendCapacity() {
// 新建一个长度为 size * extendRatio 的数组,并将原数组拷贝到新数组
int newCapacity = capacity() * extendRatio;
int* tmp = nums;
nums = new int[newCapacity];
// 将原数组中的所有元素复制到新数组
for (int i = 0; i < size(); i++) {
nums[i] = tmp[i];
}
// 释放内存
delete[] tmp;
numsCapacity = newCapacity;
}
};
[class]{MyList}-[func]{}
```
=== "Python"