build
This commit is contained in:
parent
b472215f0e
commit
cf4a59e3d6
@ -522,8 +522,7 @@ comments: true
|
||||
```javascript title="linked_list.js"
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
function remove(n0) {
|
||||
if (!n0.next)
|
||||
return;
|
||||
if (!n0.next) return;
|
||||
// n0 -> P -> n1
|
||||
const P = n0.next;
|
||||
const n1 = P.next;
|
||||
|
||||
@ -1143,15 +1143,13 @@ comments: true
|
||||
/* 访问元素 */
|
||||
get(index) {
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index < 0 || index >= this.#size)
|
||||
throw new Error('索引越界');
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
return this.#nums[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
set(index, num) {
|
||||
if (index < 0 || index >= this.#size)
|
||||
throw new Error('索引越界');
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
this.#nums[index] = num;
|
||||
}
|
||||
|
||||
@ -1168,8 +1166,7 @@ comments: true
|
||||
|
||||
/* 中间插入元素 */
|
||||
insert(index, num) {
|
||||
if (index < 0 || index >= this.#size)
|
||||
throw new Error('索引越界');
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
@ -1185,8 +1182,7 @@ comments: true
|
||||
|
||||
/* 删除元素 */
|
||||
remove(index) {
|
||||
if (index < 0 || index >= this.#size)
|
||||
throw new Error('索引越界');
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
let num = this.#nums[index];
|
||||
// 将索引 index 之后的元素都向前移动一位
|
||||
for (let j = index; j < this.#size - 1; j++) {
|
||||
@ -1249,23 +1245,20 @@ comments: true
|
||||
/* 访问元素 */
|
||||
public get(index: number): number {
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index < 0 || index >= this._size)
|
||||
throw new Error('索引越界');
|
||||
if (index < 0 || index >= this._size) throw new Error('索引越界');
|
||||
return this.nums[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
public set(index: number, num: number): void {
|
||||
if (index < 0 || index >= this._size)
|
||||
throw new Error('索引越界');
|
||||
if (index < 0 || index >= this._size) throw new Error('索引越界');
|
||||
this.nums[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
public add(num: number): void {
|
||||
// 如果长度等于容量,则需要扩容
|
||||
if (this._size === this._capacity)
|
||||
this.extendCapacity();
|
||||
if (this._size === this._capacity) this.extendCapacity();
|
||||
// 将新元素添加到列表尾部
|
||||
this.nums[this._size] = num;
|
||||
this._size++;
|
||||
@ -1273,8 +1266,7 @@ comments: true
|
||||
|
||||
/* 中间插入元素 */
|
||||
public insert(index: number, num: number): void {
|
||||
if (index < 0 || index >= this._size)
|
||||
throw new Error('索引越界');
|
||||
if (index < 0 || index >= this._size) throw new Error('索引越界');
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (this._size === this._capacity) {
|
||||
this.extendCapacity();
|
||||
@ -1290,8 +1282,7 @@ comments: true
|
||||
|
||||
/* 删除元素 */
|
||||
public remove(index: number): number {
|
||||
if (index < 0 || index >= this._size)
|
||||
throw new Error('索引越界');
|
||||
if (index < 0 || index >= this._size) throw new Error('索引越界');
|
||||
let num = this.nums[index];
|
||||
// 将索引 index 之后的元素都向前移动一位
|
||||
for (let j = index; j < this._size - 1; j++) {
|
||||
|
||||
@ -135,16 +135,19 @@ $$
|
||||
/* 二分查找(双闭区间) */
|
||||
function binarySearch(nums, target) {
|
||||
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
||||
let i = 0, j = nums.length - 1;
|
||||
let i = 0,
|
||||
j = nums.length - 1;
|
||||
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||
while (i <= j) {
|
||||
const m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
|
||||
// 计算中点索引 m ,使用 parseInt() 向下取整
|
||||
const m = parseInt((i + j) / 2);
|
||||
if (nums[m] < target)
|
||||
// 此情况说明 target 在区间 [m+1, j] 中
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
|
||||
else if (nums[m] > target)
|
||||
// 此情况说明 target 在区间 [i, m-1] 中
|
||||
j = m - 1;
|
||||
else
|
||||
return m; // 找到目标元素,返回其索引
|
||||
else return m; // 找到目标元素,返回其索引
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1;
|
||||
@ -157,15 +160,20 @@ $$
|
||||
/* 二分查找(双闭区间) */
|
||||
function binarySearch(nums: number[], target: number): number {
|
||||
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
||||
let i = 0, j = nums.length - 1;
|
||||
let i = 0,
|
||||
j = nums.length - 1;
|
||||
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||
while (i <= j) {
|
||||
const m = Math.floor((i + j) / 2); // 计算中点索引 m
|
||||
if (nums[m] < target) { // 此情况说明 target 在区间 [m+1, j] 中
|
||||
// 计算中点索引 m
|
||||
const m = Math.floor((i + j) / 2);
|
||||
if (nums[m] < target) {
|
||||
// 此情况说明 target 在区间 [m+1, j] 中
|
||||
i = m + 1;
|
||||
} else if (nums[m] > target) { // 此情况说明 target 在区间 [i, m-1] 中
|
||||
} else if (nums[m] > target) {
|
||||
// 此情况说明 target 在区间 [i, m-1] 中
|
||||
j = m - 1;
|
||||
} else { // 找到目标元素,返回其索引
|
||||
} else {
|
||||
// 找到目标元素,返回其索引
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@ -431,16 +439,20 @@ $$
|
||||
/* 二分查找(左闭右开) */
|
||||
function binarySearch1(nums, target) {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
let i = 0, j = nums.length;
|
||||
let i = 0,
|
||||
j = nums.length;
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
while (i < j) {
|
||||
const m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
|
||||
// 计算中点索引 m ,使用 parseInt() 向下取整
|
||||
const m = parseInt((i + j) / 2);
|
||||
if (nums[m] < target)
|
||||
// 此情况说明 target 在区间 [m+1, j) 中
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
|
||||
else if (nums[m] > target)
|
||||
// 此情况说明 target 在区间 [i, m) 中
|
||||
j = m;
|
||||
else // 找到目标元素,返回其索引
|
||||
return m;
|
||||
// 找到目标元素,返回其索引
|
||||
else return m;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1;
|
||||
@ -453,15 +465,20 @@ $$
|
||||
/* 二分查找(左闭右开) */
|
||||
function binarySearch1(nums: number[], target: number): number {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
let i = 0, j = nums.length;
|
||||
let i = 0,
|
||||
j = nums.length;
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
while (i < j) {
|
||||
const m = Math.floor((i + j) / 2); // 计算中点索引 m
|
||||
if (nums[m] < target) { // 此情况说明 target 在区间 [m+1, j) 中
|
||||
// 计算中点索引 m
|
||||
const m = Math.floor((i + j) / 2);
|
||||
if (nums[m] < target) {
|
||||
// 此情况说明 target 在区间 [m+1, j) 中
|
||||
i = m + 1;
|
||||
} else if (nums[m] > target) { // 此情况说明 target 在区间 [i, m) 中
|
||||
} else if (nums[m] > target) {
|
||||
// 此情况说明 target 在区间 [i, m) 中
|
||||
j = m;
|
||||
} else { // 找到目标元素,返回其索引
|
||||
} else {
|
||||
// 找到目标元素,返回其索引
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1156,7 +1156,9 @@ $$
|
||||
/* 平方阶 */
|
||||
function quadratic(n) {
|
||||
// 矩阵占用 O(n^2) 空间
|
||||
const numMatrix = Array(n).fill(null).map(() => Array(n).fill(null));
|
||||
const numMatrix = Array(n)
|
||||
.fill(null)
|
||||
.map(() => Array(n).fill(null));
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
const numList = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
|
||||
@ -434,7 +434,7 @@ comments: true
|
||||
/* 删除顶点 */
|
||||
removeVertex(index) {
|
||||
if (index >= this.size()) {
|
||||
throw new RangeError("Index Out Of Bounds Exception");
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
}
|
||||
// 在顶点列表中移除索引 index 的顶点
|
||||
this.vertices.splice(index, 1);
|
||||
@ -452,7 +452,7 @@ comments: true
|
||||
addEdge(i, j) {
|
||||
// 索引越界与相等处理
|
||||
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
|
||||
throw new RangeError("Index Out Of Bounds Exception");
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
}
|
||||
// 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)
|
||||
this.adjMat[i][j] = 1;
|
||||
@ -464,7 +464,7 @@ comments: true
|
||||
removeEdge(i, j) {
|
||||
// 索引越界与相等处理
|
||||
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
|
||||
throw new RangeError("Index Out Of Bounds Exception");
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
}
|
||||
this.adjMat[i][j] = 0;
|
||||
this.adjMat[j][i] = 0;
|
||||
@ -472,8 +472,8 @@ comments: true
|
||||
|
||||
/* 打印邻接矩阵 */
|
||||
print() {
|
||||
console.log("顶点列表 = ", this.vertices);
|
||||
console.log("邻接矩阵 =", this.adjMat);
|
||||
console.log('顶点列表 = ', this.vertices);
|
||||
console.log('邻接矩阵 =', this.adjMat);
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -526,7 +526,7 @@ comments: true
|
||||
/* 删除顶点 */
|
||||
removeVertex(index: number): void {
|
||||
if (index >= this.size()) {
|
||||
throw new RangeError("Index Out Of Bounds Exception");
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
}
|
||||
// 在顶点列表中移除索引 index 的顶点
|
||||
this.vertices.splice(index, 1);
|
||||
@ -544,7 +544,7 @@ comments: true
|
||||
addEdge(i: number, j: number): void {
|
||||
// 索引越界与相等处理
|
||||
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
|
||||
throw new RangeError("Index Out Of Bounds Exception");
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
}
|
||||
// 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)
|
||||
this.adjMat[i][j] = 1;
|
||||
@ -556,7 +556,7 @@ comments: true
|
||||
removeEdge(i: number, j: number): void {
|
||||
// 索引越界与相等处理
|
||||
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
|
||||
throw new RangeError("Index Out Of Bounds Exception");
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
}
|
||||
this.adjMat[i][j] = 0;
|
||||
this.adjMat[j][i] = 0;
|
||||
@ -564,8 +564,8 @@ comments: true
|
||||
|
||||
/* 打印邻接矩阵 */
|
||||
print(): void {
|
||||
console.log("顶点列表 = ", this.vertices);
|
||||
console.log("邻接矩阵 =", this.adjMat);
|
||||
console.log('顶点列表 = ', this.vertices);
|
||||
console.log('邻接矩阵 =', this.adjMat);
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -1145,8 +1145,12 @@ comments: true
|
||||
|
||||
/* 添加边 */
|
||||
addEdge(vet1, vet2) {
|
||||
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
|
||||
throw new Error("Illegal Argument Exception");
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
!this.adjList.has(vet2) ||
|
||||
vet1 === vet2
|
||||
) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
}
|
||||
// 添加边 vet1 - vet2
|
||||
this.adjList.get(vet1).push(vet2);
|
||||
@ -1155,8 +1159,12 @@ comments: true
|
||||
|
||||
/* 删除边 */
|
||||
removeEdge(vet1, vet2) {
|
||||
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
|
||||
throw new Error("Illegal Argument Exception");
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
!this.adjList.has(vet2) ||
|
||||
vet1 === vet2
|
||||
) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
}
|
||||
// 删除边 vet1 - vet2
|
||||
this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);
|
||||
@ -1173,7 +1181,7 @@ comments: true
|
||||
/* 删除顶点 */
|
||||
removeVertex(vet) {
|
||||
if (!this.adjList.has(vet)) {
|
||||
throw new Error("Illegal Argument Exception");
|
||||
throw new Error('Illegal Argument Exception');
|
||||
}
|
||||
// 在邻接表中删除顶点 vet 对应的链表
|
||||
this.adjList.delete(vet);
|
||||
@ -1188,13 +1196,13 @@ comments: true
|
||||
|
||||
/* 打印邻接表 */
|
||||
print() {
|
||||
console.log("邻接表 =");
|
||||
console.log('邻接表 =');
|
||||
for (const [key, value] of this.adjList) {
|
||||
const tmp = [];
|
||||
for (const vertex of value) {
|
||||
tmp.push(vertex.val);
|
||||
}
|
||||
console.log(key.val + ": " + tmp.join());
|
||||
console.log(key.val + ': ' + tmp.join());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1226,7 +1234,11 @@ comments: true
|
||||
|
||||
/* 添加边 */
|
||||
addEdge(vet1: Vertex, vet2: Vertex): void {
|
||||
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
!this.adjList.has(vet2) ||
|
||||
vet1 === vet2
|
||||
) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
}
|
||||
// 添加边 vet1 - vet2
|
||||
@ -1236,7 +1248,11 @@ comments: true
|
||||
|
||||
/* 删除边 */
|
||||
removeEdge(vet1: Vertex, vet2: Vertex): void {
|
||||
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
!this.adjList.has(vet2) ||
|
||||
vet1 === vet2
|
||||
) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
}
|
||||
// 删除边 vet1 - vet2
|
||||
|
||||
@ -493,7 +493,12 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
|
||||
|
||||
```typescript title="graph_dfs.ts"
|
||||
/* 深度优先遍历 DFS 辅助函数 */
|
||||
function dfs(graph: GraphAdjList, visited: Set<Vertex>, res: Vertex[], vet: Vertex): void {
|
||||
function dfs(
|
||||
graph: GraphAdjList,
|
||||
visited: Set<Vertex>,
|
||||
res: Vertex[],
|
||||
vet: Vertex
|
||||
): void {
|
||||
res.push(vet); // 记录访问顶点
|
||||
visited.add(vet); // 标记该顶点已被访问
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
|
||||
@ -882,12 +882,11 @@ $$
|
||||
|
||||
/* 基于数组简易实现的哈希表 */
|
||||
class ArrayHashMap {
|
||||
|
||||
private readonly buckets: (Entry | null)[];
|
||||
|
||||
constructor() {
|
||||
// 初始化数组,包含 100 个桶
|
||||
this.buckets = (new Array(100)).fill(null);
|
||||
this.buckets = new Array(100).fill(null);
|
||||
}
|
||||
|
||||
/* 哈希函数 */
|
||||
|
||||
@ -1079,7 +1079,7 @@ comments: true
|
||||
/* 元素出堆 */
|
||||
pop() {
|
||||
// 判空处理
|
||||
if (this.isEmpty()) throw new Error("堆为空");
|
||||
if (this.isEmpty()) throw new Error('堆为空');
|
||||
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||
this.#swap(0, this.size() - 1);
|
||||
// 删除节点
|
||||
|
||||
@ -118,7 +118,7 @@ comments: true
|
||||
}
|
||||
}
|
||||
return [];
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
@ -302,7 +302,7 @@ comments: true
|
||||
}
|
||||
}
|
||||
return [];
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
@ -102,7 +102,8 @@ comments: true
|
||||
function insertionSort(nums) {
|
||||
// 外循环:base = nums[1], nums[2], ..., nums[n-1]
|
||||
for (let i = 1; i < nums.length; i++) {
|
||||
let base = nums[i], j = i - 1;
|
||||
let base = nums[i],
|
||||
j = i - 1;
|
||||
// 内循环:将 base 插入到左边的正确位置
|
||||
while (j >= 0 && nums[j] > base) {
|
||||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
|
||||
|
||||
@ -250,21 +250,24 @@ comments: true
|
||||
// 初始化辅助数组
|
||||
let tmp = nums.slice(left, right + 1);
|
||||
// 左子数组的起始索引和结束索引
|
||||
let leftStart = left - left, leftEnd = mid - left;
|
||||
let leftStart = left - left,
|
||||
leftEnd = mid - left;
|
||||
// 右子数组的起始索引和结束索引
|
||||
let rightStart = mid + 1 - left, rightEnd = right - left;
|
||||
let rightStart = mid + 1 - left,
|
||||
rightEnd = right - left;
|
||||
// i, j 分别指向左子数组、右子数组的首元素
|
||||
let i = leftStart, j = rightStart;
|
||||
let i = leftStart,
|
||||
j = rightStart;
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for (let k = left; k <= right; k++) {
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if (i > leftEnd) {
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
nums[k] = tmp[j++];
|
||||
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
} else if (j > rightEnd || tmp[i] <= tmp[j]) {
|
||||
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
nums[k] = tmp[i++];
|
||||
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
} else {
|
||||
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
nums[k] = tmp[j++];
|
||||
}
|
||||
}
|
||||
@ -293,15 +296,18 @@ comments: true
|
||||
// 初始化辅助数组
|
||||
let tmp = nums.slice(left, right + 1);
|
||||
// 左子数组的起始索引和结束索引
|
||||
let leftStart = left - left, leftEnd = mid - left;
|
||||
let leftStart = left - left,
|
||||
leftEnd = mid - left;
|
||||
// 右子数组的起始索引和结束索引
|
||||
let rightStart = mid + 1 - left, rightEnd = right - left;
|
||||
let rightStart = mid + 1 - left,
|
||||
rightEnd = right - left;
|
||||
// i, j 分别指向左子数组、右子数组的首元素
|
||||
let i = leftStart, j = rightStart;
|
||||
let i = leftStart,
|
||||
j = rightStart;
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for (let k = left; k <= right; k++) {
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if (i > leftEnd) {
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
nums[k] = tmp[j++];
|
||||
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
} else if (j > rightEnd || tmp[i] <= tmp[j]) {
|
||||
|
||||
@ -152,7 +152,8 @@ comments: true
|
||||
/* 哨兵划分 */
|
||||
partition(nums, left, right) {
|
||||
// 以 nums[left] 作为基准数
|
||||
let i = left, j = right;
|
||||
let i = left,
|
||||
j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left]) {
|
||||
j -= 1; // 从右向左找首个小于基准数的元素
|
||||
@ -181,7 +182,8 @@ comments: true
|
||||
/* 哨兵划分 */
|
||||
partition(nums: number[], left: number, right: number): number {
|
||||
// 以 nums[left] 作为基准数
|
||||
let i = left, j = right;
|
||||
let i = left,
|
||||
j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left]) {
|
||||
j -= 1; // 从右向左找首个小于基准数的元素
|
||||
@ -625,18 +627,25 @@ comments: true
|
||||
// 此处使用异或运算来简化代码
|
||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
||||
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left;
|
||||
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid;
|
||||
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
|
||||
return mid;
|
||||
else return right;
|
||||
}
|
||||
|
||||
/* 哨兵划分(三数取中值) */
|
||||
partition(nums, left, right) {
|
||||
// 选取三个候选元素的中位数
|
||||
let med = this.medianThree(nums, left, Math.floor((left + right) / 2), right);
|
||||
let med = this.medianThree(
|
||||
nums,
|
||||
left,
|
||||
Math.floor((left + right) / 2),
|
||||
right
|
||||
);
|
||||
// 将中位数交换至数组最左端
|
||||
this.swap(nums, left, med);
|
||||
// 以 nums[left] 作为基准数
|
||||
let i = left, j = right;
|
||||
let i = left,
|
||||
j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left]) j--; // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left]) i++; // 从左向右找首个大于基准数的元素
|
||||
@ -651,12 +660,19 @@ comments: true
|
||||
|
||||
```typescript title="quick_sort.ts"
|
||||
/* 选取三个元素的中位数 */
|
||||
medianThree(nums: number[], left: number, mid: number, right: number): number {
|
||||
medianThree(
|
||||
nums: number[],
|
||||
left: number,
|
||||
mid: number,
|
||||
right: number
|
||||
): number {
|
||||
// 此处使用异或运算来简化代码
|
||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
||||
if (Number(nums[left] < nums[mid]) ^ Number(nums[left] < nums[right])) {
|
||||
return left;
|
||||
} else if (Number(nums[mid] < nums[left]) ^ Number(nums[mid] < nums[right])) {
|
||||
} else if (
|
||||
Number(nums[mid] < nums[left]) ^ Number(nums[mid] < nums[right])
|
||||
) {
|
||||
return mid;
|
||||
} else {
|
||||
return right;
|
||||
@ -666,11 +682,17 @@ comments: true
|
||||
/* 哨兵划分(三数取中值) */
|
||||
partition(nums: number[], left: number, right: number): number {
|
||||
// 选取三个候选元素的中位数
|
||||
let med = this.medianThree(nums, left, Math.floor((left + right) / 2), right);
|
||||
let med = this.medianThree(
|
||||
nums,
|
||||
left,
|
||||
Math.floor((left + right) / 2),
|
||||
right
|
||||
);
|
||||
// 将中位数交换至数组最左端
|
||||
this.swap(nums, left, med);
|
||||
// 以 nums[left] 作为基准数
|
||||
let i = left, j = right;
|
||||
let i = left,
|
||||
j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left]) {
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
|
||||
@ -901,7 +901,7 @@ comments: true
|
||||
arr.push(temp.val);
|
||||
temp = temp.next;
|
||||
}
|
||||
console.log("[" + arr.join(", ") + "]");
|
||||
console.log('[' + arr.join(', ') + ']');
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -1028,7 +1028,7 @@ comments: true
|
||||
arr.push(temp.val);
|
||||
temp = temp.next;
|
||||
}
|
||||
console.log("[" + arr.join(", ") + "]");
|
||||
console.log('[' + arr.join(', ') + ']');
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -1827,7 +1827,7 @@ comments: true
|
||||
/* 队首入队 */
|
||||
pushFirst(num) {
|
||||
if (this.#queSize === this.capacity()) {
|
||||
console.log("双向队列已满");
|
||||
console.log('双向队列已满');
|
||||
return;
|
||||
}
|
||||
// 队首指针向左移动一位
|
||||
@ -1841,7 +1841,7 @@ comments: true
|
||||
/* 队尾入队 */
|
||||
pushLast(num) {
|
||||
if (this.#queSize === this.capacity()) {
|
||||
console.log("双向队列已满");
|
||||
console.log('双向队列已满');
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
@ -1869,15 +1869,13 @@ comments: true
|
||||
|
||||
/* 访问队首元素 */
|
||||
peekFirst() {
|
||||
if (this.isEmpty())
|
||||
throw new Error("The Deque Is Empty.");
|
||||
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
peekLast() {
|
||||
if (this.isEmpty())
|
||||
throw new Error("The Deque Is Empty.");
|
||||
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
|
||||
// 计算尾元素索引
|
||||
const last = this.index(this.#front + this.#queSize - 1);
|
||||
return this.#nums[last];
|
||||
@ -1937,7 +1935,7 @@ comments: true
|
||||
/* 队首入队 */
|
||||
pushFirst(num: number): void {
|
||||
if (this.queSize === this.capacity()) {
|
||||
console.log("双向队列已满");
|
||||
console.log('双向队列已满');
|
||||
return;
|
||||
}
|
||||
// 队首指针向左移动一位
|
||||
@ -1951,7 +1949,7 @@ comments: true
|
||||
/* 队尾入队 */
|
||||
pushLast(num: number): void {
|
||||
if (this.queSize === this.capacity()) {
|
||||
console.log("双向队列已满");
|
||||
console.log('双向队列已满');
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
@ -1979,15 +1977,13 @@ comments: true
|
||||
|
||||
/* 访问队首元素 */
|
||||
peekFirst(): number {
|
||||
if (this.isEmpty())
|
||||
throw new Error("The Deque Is Empty.");
|
||||
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
|
||||
return this.nums[this.front];
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
peekLast(): number {
|
||||
if (this.isEmpty())
|
||||
throw new Error("The Deque Is Empty.");
|
||||
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
|
||||
// 计算尾元素索引
|
||||
const last = this.index(this.front + this.queSize - 1);
|
||||
return this.nums[last];
|
||||
|
||||
@ -590,8 +590,7 @@ comments: true
|
||||
|
||||
/* 访问队首元素 */
|
||||
peek() {
|
||||
if (this.size === 0)
|
||||
throw new Error("队列为空");
|
||||
if (this.size === 0) throw new Error('队列为空');
|
||||
return this.#front.val;
|
||||
}
|
||||
|
||||
@ -1261,7 +1260,7 @@ comments: true
|
||||
/* 入队 */
|
||||
push(num) {
|
||||
if (this.size == this.capacity) {
|
||||
console.log("队列已满");
|
||||
console.log('队列已满');
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
@ -1283,8 +1282,7 @@ comments: true
|
||||
|
||||
/* 访问队首元素 */
|
||||
peek() {
|
||||
if (this.empty())
|
||||
throw new Error("队列为空");
|
||||
if (this.empty()) throw new Error('队列为空');
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
@ -1332,7 +1330,7 @@ comments: true
|
||||
/* 入队 */
|
||||
push(num: number): void {
|
||||
if (this.size == this.capacity) {
|
||||
console.log("队列已满");
|
||||
console.log('队列已满');
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
@ -1354,8 +1352,7 @@ comments: true
|
||||
|
||||
/* 访问队首元素 */
|
||||
peek(): number {
|
||||
if (this.empty())
|
||||
throw new Error("队列为空");
|
||||
if (this.empty()) throw new Error('队列为空');
|
||||
return this.nums[this.front];
|
||||
}
|
||||
|
||||
|
||||
@ -552,8 +552,7 @@ comments: true
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
peek() {
|
||||
if (!this.#stackPeek)
|
||||
throw new Error("栈为空");
|
||||
if (!this.#stackPeek) throw new Error('栈为空');
|
||||
return this.#stackPeek.val;
|
||||
}
|
||||
|
||||
@ -1068,15 +1067,13 @@ comments: true
|
||||
|
||||
/* 出栈 */
|
||||
pop() {
|
||||
if (this.empty())
|
||||
throw new Error("栈为空");
|
||||
if (this.empty()) throw new Error('栈为空');
|
||||
return this.#stack.pop();
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
top() {
|
||||
if (this.empty())
|
||||
throw new Error("栈为空");
|
||||
if (this.empty()) throw new Error('栈为空');
|
||||
return this.#stack[this.#stack.length - 1];
|
||||
}
|
||||
|
||||
@ -1084,7 +1081,7 @@ comments: true
|
||||
toArray() {
|
||||
return this.#stack;
|
||||
}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@ -1114,15 +1111,13 @@ comments: true
|
||||
|
||||
/* 出栈 */
|
||||
pop(): number | undefined {
|
||||
if (this.empty())
|
||||
throw new Error('栈为空');
|
||||
if (this.empty()) throw new Error('栈为空');
|
||||
return this.stack.pop();
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
top(): number | undefined {
|
||||
if (this.empty())
|
||||
throw new Error('栈为空');
|
||||
if (this.empty()) throw new Error('栈为空');
|
||||
return this.stack[this.stack.length - 1];
|
||||
}
|
||||
|
||||
@ -1130,7 +1125,7 @@ comments: true
|
||||
toArray() {
|
||||
return this.stack;
|
||||
}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
@ -242,7 +242,8 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
/* 更新节点高度 */
|
||||
#updateHeight(node) {
|
||||
// 节点高度等于最高子树高度 + 1
|
||||
node.height = Math.max(this.height(node.left), this.height(node.right)) + 1;
|
||||
node.height =
|
||||
Math.max(this.height(node.left), this.height(node.right)) + 1;
|
||||
}
|
||||
```
|
||||
|
||||
@ -258,7 +259,8 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
/* 更新节点高度 */
|
||||
updateHeight(node: TreeNode): void {
|
||||
// 节点高度等于最高子树高度 + 1
|
||||
node.height = Math.max(this.height(node.left), this.height(node.right)) + 1;
|
||||
node.height =
|
||||
Math.max(this.height(node.left), this.height(node.right)) + 1;
|
||||
}
|
||||
```
|
||||
|
||||
@ -1315,7 +1317,8 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
if (node === null) return new TreeNode(val);
|
||||
/* 1. 查找插入位置,并插入节点 */
|
||||
if (val < node.val) node.left = this.#insertHelper(node.left, val);
|
||||
else if (val > node.val) node.right = this.#insertHelper(node.right, val);
|
||||
else if (val > node.val)
|
||||
node.right = this.#insertHelper(node.right, val);
|
||||
else return node; // 重复节点不插入,直接返回
|
||||
this.#updateHeight(node); // 更新节点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
@ -1649,7 +1652,8 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
if (node === null) return null;
|
||||
/* 1. 查找节点,并删除之 */
|
||||
if (val < node.val) node.left = this.#removeHelper(node.left, val);
|
||||
else if (val > node.val) node.right = this.#removeHelper(node.right, val);
|
||||
else if (val > node.val)
|
||||
node.right = this.#removeHelper(node.right, val);
|
||||
else {
|
||||
if (node.left === null || node.right === null) {
|
||||
const child = node.left !== null ? node.left : node.right;
|
||||
|
||||
@ -393,7 +393,8 @@ comments: true
|
||||
function insert(num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root === null) return;
|
||||
let cur = root, pre = null;
|
||||
let cur = root,
|
||||
pre = null;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur !== null) {
|
||||
// 找到重复节点,直接返回
|
||||
@ -808,7 +809,8 @@ comments: true
|
||||
function remove(num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root === null) return;
|
||||
let cur = root, pre = null;
|
||||
let cur = root,
|
||||
pre = null;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur !== null) {
|
||||
// 找到待删除节点,跳出循环
|
||||
|
||||
@ -126,11 +126,8 @@ comments: true
|
||||
while (queue.length) {
|
||||
let node = queue.shift(); // 队列出队
|
||||
list.push(node.val); // 保存节点值
|
||||
if (node.left)
|
||||
queue.push(node.left); // 左子节点入队
|
||||
if (node.right)
|
||||
queue.push(node.right); // 右子节点入队
|
||||
|
||||
if (node.left) queue.push(node.left); // 左子节点入队
|
||||
if (node.right) queue.push(node.right); // 右子节点入队
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user