This commit is contained in:
krahets
2023-04-14 04:01:38 +08:00
parent cf431646e9
commit 4d318e8e6b
26 changed files with 469 additions and 295 deletions
+8 -9
View File
@@ -125,8 +125,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
/* 随机返回一个数组元素 */
int randomAccess(int[] nums) {
// 在区间 [0, nums.length) 中随机抽取一个数字
int randomIndex = ThreadLocalRandom.current().
nextInt(0, nums.length);
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
// 获取并返回随机元素
int randomNum = nums[randomIndex];
return randomNum;
@@ -137,7 +136,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
```cpp title="array.cpp"
/* 随机返回一个数组元素 */
int randomAccess(int* nums, int size) {
int randomAccess(int *nums, int size) {
// 在区间 [0, size) 中随机抽取一个数字
int randomIndex = rand() % size;
// 获取并返回随机元素
@@ -268,9 +267,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
```cpp title="array.cpp"
/* 扩展数组长度 */
int* extend(int* nums, int size, int enlarge) {
int *extend(int *nums, int size, int enlarge) {
// 初始化一个扩展长度后的数组
int* res = new int[size + enlarge];
int *res = new int[size + enlarge];
// 将原数组中的所有元素复制到新数组
for (int i = 0; i < size; i++) {
res[i] = nums[i];
@@ -427,7 +426,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
```cpp title="array.cpp"
/* 在数组的索引 index 处插入元素 num */
void insert(int* nums, int size, int num, int index) {
void insert(int *nums, int size, int num, int index) {
// 把索引 index 以及之后的所有元素向后移动一位
for (int i = size - 1; i > index; i--) {
nums[i] = nums[i - 1];
@@ -549,7 +548,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
```cpp title="array.cpp"
/* 删除索引 index 处元素 */
void remove(int* nums, int size, int index) {
void remove(int *nums, int size, int index) {
// 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < size - 1; i++) {
nums[i] = nums[i + 1];
@@ -680,7 +679,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
```cpp title="array.cpp"
/* 遍历数组 */
void traverse(int* nums, int size) {
void traverse(int *nums, int size) {
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < size; i++) {
@@ -836,7 +835,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
```cpp title="array.cpp"
/* 在数组中查找指定元素 */
int find(int* nums, int size, int target) {
int find(int *nums, int size, int target) {
for (int i = 0; i < size; i++) {
if (nums[i] == target)
return i;
+7 -7
View File
@@ -363,8 +363,8 @@ comments: true
```cpp title="linked_list.cpp"
/* 在链表的节点 n0 之后插入节点 P */
void insert(ListNode* n0, ListNode* P) {
ListNode* n1 = n0->next;
void insert(ListNode *n0, ListNode *P) {
ListNode *n1 = n0->next;
P->next = n1;
n0->next = P;
}
@@ -477,12 +477,12 @@ comments: true
```cpp title="linked_list.cpp"
/* 删除链表的节点 n0 之后的首个节点 */
void remove(ListNode* n0) {
void remove(ListNode *n0) {
if (n0->next == nullptr)
return;
// n0 -> P -> n1
ListNode* P = n0->next;
ListNode* n1 = P->next;
ListNode *P = n0->next;
ListNode *n1 = P->next;
n0->next = n1;
// 释放内存
delete P;
@@ -618,7 +618,7 @@ comments: true
```cpp title="linked_list.cpp"
/* 访问链表中索引为 index 的节点 */
ListNode* access(ListNode* head, int index) {
ListNode *access(ListNode *head, int index) {
for (int i = 0; i < index; i++) {
if (head == nullptr)
return nullptr;
@@ -764,7 +764,7 @@ comments: true
```cpp title="linked_list.cpp"
/* 在链表中查找值为 target 的首个节点 */
int find(ListNode* head, int target) {
int find(ListNode *head, int target) {
int index = 0;
while (head != nullptr) {
if (head->val == target)
+12 -12
View File
@@ -718,17 +718,17 @@ comments: true
```java title="my_list.java"
/* 列表类简易实现 */
class MyList {
private int[] nums; // 数组(存储列表元素)
private int capacity = 10; // 列表容量
private int size = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数
private int[] nums; // 数组(存储列表元素)
private int capacity = 10; // 列表容量
private int size = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数
/* 构造方法 */
public MyList() {
nums = new int[capacity];
}
/* 获取列表长度(即当前元素数量)*/
/* 获取列表长度(即当前元素数量) */
public int size() {
return size;
}
@@ -820,13 +820,13 @@ comments: true
```cpp title="my_list.cpp"
/* 列表类简易实现 */
class MyList {
private:
int* nums; // 数组(存储列表元素)
int numsCapacity = 10; // 列表容量
int numsSize = 0; // 列表长度(即当前元素数量)
int extendRatio = 2; // 每次列表扩容的倍数
private:
int *nums; // 数组(存储列表元素)
int numsCapacity = 10; // 列表容量
int numsSize = 0; // 列表长度(即当前元素数量)
int extendRatio = 2; // 每次列表扩容的倍数
public:
public:
/* 构造方法 */
MyList() {
nums = new int[numsCapacity];
@@ -907,7 +907,7 @@ comments: true
void extendCapacity() {
// 新建一个长度为 size * extendRatio 的数组,并将原数组拷贝到新数组
int newCapacity = capacity() * extendRatio;
int* tmp = nums;
int *tmp = nums;
nums = new int[newCapacity];
// 将原数组中的所有元素复制到新数组
for (int i = 0; i < size(); i++) {