This commit is contained in:
krahets
2023-02-08 16:47:52 +08:00
parent 139e34bdb1
commit 7156a5f832
15 changed files with 367 additions and 208 deletions
+7 -3
View File
@@ -313,6 +313,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
```javascript title="array.js"
/* 扩展数组长度 */
// 请注意,JavaScript 的 Array 是动态数组,可以直接扩展
// 为了方便学习,本函数将 Array 看作是长度不可变的数组
function extend(nums, enlarge) {
// 初始化一个扩展长度后的数组
const res = new Array(nums.length + enlarge).fill(0);
@@ -329,6 +331,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
```typescript title="array.ts"
/* 扩展数组长度 */
// 请注意,TypeScript 的 Array 是动态数组,可以直接扩展
// 为了方便学习,本函数将 Array 看作是长度不可变的数组
function extend(nums: number[], enlarge: number): number[] {
// 初始化一个扩展长度后的数组
const res = new Array(nums.length + enlarge).fill(0);
@@ -502,7 +506,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
function remove(nums, index) {
// 把索引 index 之后的所有元素向前移动一位
@@ -524,7 +528,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
function remove(nums: number[], index: number): void {
// 把索引 index 之后的所有元素向前移动一位
@@ -705,7 +709,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
count++;
}
// 直接遍历数组
for(let num of nums){
for (let num of nums) {
count += 1;
}
}
+2 -1
View File
@@ -587,8 +587,9 @@ comments: true
/* 访问链表中索引为 index 的结点 */
function access(head, index) {
for (let i = 0; i < index; i++) {
if (!head)
if (!head) {
return null;
}
head = head.next;
}
return head;
+22
View File
@@ -1195,6 +1195,17 @@ comments: true
// 更新列表容量
this.#capacity = this.#nums.length;
}
/* 将列表转换为数组 */
toArray() {
let size = this.size();
// 仅转换有效长度范围内的列表元素
const nums = new Array(size);
for (let i = 0; i < size; i++) {
nums[i] = this.get(i);
}
return nums;
}
}
```
@@ -1289,6 +1300,17 @@ comments: true
// 更新列表容量
this._capacity = this.nums.length;
}
/* 将列表转换为数组 */
public toArray(): number[] {
let size = this.size();
// 仅转换有效长度范围内的列表元素
let nums = new Array(size);
for (let i = 0; i < size; i++) {
nums[i] = this.get(i);
}
return nums;
}
}
```