mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
feat: Revised the book (#978)
* Sync recent changes to the revised Word. * Revised the preface chapter * Revised the introduction chapter * Revised the computation complexity chapter * Revised the chapter data structure * Revised the chapter array and linked list * Revised the chapter stack and queue * Revised the chapter hashing * Revised the chapter tree * Revised the chapter heap * Revised the chapter graph * Revised the chapter searching * Reivised the sorting chapter * Revised the divide and conquer chapter * Revised the chapter backtacking * Revised the DP chapter * Revised the greedy chapter * Revised the appendix chapter * Revised the preface chapter doubly * Revised the figures
This commit is contained in:
@ -15,7 +15,7 @@ function randomAccess(nums) {
|
||||
|
||||
/* 扩展数组长度 */
|
||||
// 请注意,JavaScript 的 Array 是动态数组,可以直接扩展
|
||||
// 为了方便学习,本函数将 Array 看作是长度不可变的数组
|
||||
// 为了方便学习,本函数将 Array 看作长度不可变的数组
|
||||
function extend(nums, enlarge) {
|
||||
// 初始化一个扩展长度后的数组
|
||||
const res = new Array(nums.length + enlarge).fill(0);
|
||||
@ -33,11 +33,11 @@ function insert(nums, num, index) {
|
||||
for (let i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
// 将 num 赋给 index 处的元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
/* 删除索引 index 处的元素 */
|
||||
function remove(nums, index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (let i = index; i < nums.length - 1; i++) {
|
||||
|
||||
@ -55,7 +55,7 @@ const n1 = new ListNode(3);
|
||||
const n2 = new ListNode(2);
|
||||
const n3 = new ListNode(5);
|
||||
const n4 = new ListNode(4);
|
||||
// 构建引用指向
|
||||
// 构建节点之间的引用
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
|
||||
@ -20,7 +20,7 @@ console.log(`将索引 1 处的元素更新为 0 ,得到 nums = ${nums}`);
|
||||
nums.length = 0;
|
||||
console.log(`清空列表后 nums = ${nums}`);
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.push(1);
|
||||
nums.push(3);
|
||||
nums.push(2);
|
||||
@ -28,7 +28,7 @@ nums.push(5);
|
||||
nums.push(4);
|
||||
console.log(`添加元素后 nums = ${nums}`);
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.splice(3, 0, 6);
|
||||
console.log(`在索引 3 处插入数字 6 ,得到 nums = ${nums}`);
|
||||
|
||||
|
||||
@ -4,11 +4,11 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* 列表类简易实现 */
|
||||
/* 列表类 */
|
||||
class MyList {
|
||||
#arr = new Array(); // 数组(存储列表元素)
|
||||
#capacity = 10; // 列表容量
|
||||
#size = 0; // 列表长度(即当前元素数量)
|
||||
#size = 0; // 列表长度(当前元素数量)
|
||||
#extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造方法 */
|
||||
@ -16,7 +16,7 @@ class MyList {
|
||||
this.#arr = new Array(this.#capacity);
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
/* 获取列表长度(当前元素数量)*/
|
||||
size() {
|
||||
return this.#size;
|
||||
}
|
||||
@ -39,7 +39,7 @@ class MyList {
|
||||
this.#arr[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
add(num) {
|
||||
// 如果长度等于容量,则需要扩容
|
||||
if (this.#size === this.#capacity) {
|
||||
@ -50,7 +50,7 @@ class MyList {
|
||||
this.#size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
insert(index, num) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
@ -105,7 +105,7 @@ class MyList {
|
||||
/* Driver Code */
|
||||
/* 初始化列表 */
|
||||
const nums = new MyList();
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
@ -115,7 +115,7 @@ console.log(
|
||||
`列表 nums = ${nums.toArray()} ,容量 = ${nums.capacity()} ,长度 = ${nums.size()}`
|
||||
);
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.insert(3, 6);
|
||||
console.log(`在索引 3 处插入数字 6 ,得到 nums = ${nums.toArray()}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user