From ac7ab1e01a1676c7dff5b4a1e5463a251b4210e3 Mon Sep 17 00:00:00 2001 From: krahets Date: Fri, 3 Mar 2023 21:53:50 +0800 Subject: [PATCH] build --- .../time_complexity.md | 14 +- chapter_stack_and_queue/deque.md | 214 +++++++++++++++++- 2 files changed, 218 insertions(+), 10 deletions(-) diff --git a/chapter_computational_complexity/time_complexity.md b/chapter_computational_complexity/time_complexity.md index 585b5575e..abe5b20ce 100755 --- a/chapter_computational_complexity/time_complexity.md +++ b/chapter_computational_complexity/time_complexity.md @@ -142,7 +142,7 @@ $$ ```swift title="" // 在某运行平台下 - func algorithm(_ n: Int) { + func algorithm(n: Int) { var a = 2 // 1 ns a = a + 1 // 1 ns a = a * 2 // 10 ns @@ -344,19 +344,19 @@ $$ ```swift title="" // 算法 A 时间复杂度:常数阶 - func algorithmA(_ n: Int) { + func algorithmA(n: Int) { print(0) } // 算法 B 时间复杂度:线性阶 - func algorithmB(_ n: Int) { + func algorithmB(n: Int) { for _ in 0 ..< n { print(0) } } // 算法 C 时间复杂度:常数阶 - func algorithmC(_ n: Int) { + func algorithmC(n: Int) { for _ in 0 ..< 1000000 { print(0) } @@ -1961,7 +1961,7 @@ $$ ```swift title="time_complexity.swift" /* 对数阶(循环实现) */ - func logarithmic(n: Int) -> Int { + func logarithmic(n: Double) -> Int { var count = 0 var n = n while n > 1 { @@ -2076,7 +2076,7 @@ $$ ```swift title="time_complexity.swift" /* 对数阶(递归实现) */ - func logRecur(n: Int) -> Int { + func logRecur(n: Double) -> Int { if n <= 1 { return 0 } @@ -2220,7 +2220,7 @@ $$ return 1 } var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2) - for _ in 0 ..< Int(n) { + for _ in sequence(first: 0, next: { $0 < n - 1 ? $0 + 1 : nil }) { count += 1 } return count diff --git a/chapter_stack_and_queue/deque.md b/chapter_stack_and_queue/deque.md index 2ca1db7cf..3bbec5c34 100644 --- a/chapter_stack_and_queue/deque.md +++ b/chapter_stack_and_queue/deque.md @@ -1687,14 +1687,222 @@ comments: true === "JavaScript" - ```js title="array_deque.js" - [class]{ArrayDeque}-[func]{} + ```javascript title="array_deque.js" + /* 基于环形数组实现的双向队列 */ + class ArrayDeque { + #nums; // 用于存储双向队列元素的数组 + #front; // 队首指针,指向队首元素 + #queSize; // 双向队列长度 + + /* 构造方法 */ + constructor(capacity) { + this.#nums = new Array(capacity); + this.#front = 0; + this.#queSize = 0; + } + + /* 获取双向队列的容量 */ + capacity() { + return this.#nums.length; + } + + /* 获取双向队列的长度 */ + size() { + return this.#queSize; + } + + /* 判断双向队列是否为空 */ + isEmpty() { + return this.#queSize === 0; + } + + /* 计算环形数组索引 */ + index(i) { + // 通过取余操作实现数组首尾相连 + // 当 i 越过数组尾部后,回到头部 + // 当 i 越过数组头部后,回到尾部 + return (i + this.capacity()) % this.capacity(); + } + + /* 队首入队 */ + pushFirst(num) { + if (this.#queSize === this.capacity()) { + console.log("双向队列已满"); + return; + } + // 队首指针向左移动一位 + // 通过取余操作,实现 front 越过数组头部后回到尾部 + this.#front = this.index(this.#front - 1); + // 将 num 添加至队首 + this.#nums[this.#front] = num; + this.#queSize++; + } + + /* 队尾入队 */ + pushLast(num) { + if (this.#queSize === this.capacity()) { + console.log("双向队列已满"); + return; + } + // 计算尾指针,指向队尾索引 + 1 + const rear = this.index(this.#front + this.#queSize); + // 将 num 添加至队尾 + this.#nums[rear] = num; + this.#queSize++; + } + + /* 队首出队 */ + pollFirst() { + const num = this.peekFirst(); + // 队首指针向后移动一位 + this.#front = this.index(this.#front + 1); + this.#queSize--; + return num; + } + + /* 队尾出队 */ + pollLast() { + const num = this.peekLast(); + this.#queSize--; + return num; + } + + /* 访问队首元素 */ + peekFirst() { + 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."); + // 计算尾元素索引 + const last = this.index(this.#front + this.#queSize - 1); + return this.#nums[last]; + } + + /* 返回数组用于打印 */ + toArray() { + // 仅转换有效长度范围内的列表元素 + const res = []; + for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) { + res[i] = this.#nums[this.index(j)]; + } + return res; + } + } ``` === "TypeScript" ```typescript title="array_deque.ts" - [class]{ArrayDeque}-[func]{} + /* 基于环形数组实现的双向队列 */ + class ArrayDeque { + private nums: number[]; // 用于存储双向队列元素的数组 + private front: number; // 队首指针,指向队首元素 + private queSize: number; // 双向队列长度 + + /* 构造方法 */ + constructor(capacity: number) { + this.nums = new Array(capacity); + this.front = 0; + this.queSize = 0; + } + + /* 获取双向队列的容量 */ + capacity(): number { + return this.nums.length; + } + + /* 获取双向队列的长度 */ + size(): number { + return this.queSize; + } + + /* 判断双向队列是否为空 */ + isEmpty(): boolean { + return this.queSize === 0; + } + + /* 计算环形数组索引 */ + index(i: number): number { + // 通过取余操作实现数组首尾相连 + // 当 i 越过数组尾部后,回到头部 + // 当 i 越过数组头部后,回到尾部 + return (i + this.capacity()) % this.capacity(); + } + + /* 队首入队 */ + pushFirst(num: number): void { + if (this.queSize === this.capacity()) { + console.log("双向队列已满"); + return; + } + // 队首指针向左移动一位 + // 通过取余操作,实现 front 越过数组头部后回到尾部 + this.front = this.index(this.front - 1); + // 将 num 添加至队首 + this.nums[this.front] = num; + this.queSize++; + } + + /* 队尾入队 */ + pushLast(num: number): void { + if (this.queSize === this.capacity()) { + console.log("双向队列已满"); + return; + } + // 计算尾指针,指向队尾索引 + 1 + const rear: number = this.index(this.front + this.queSize); + // 将 num 添加至队尾 + this.nums[rear] = num; + this.queSize++; + } + + /* 队首出队 */ + pollFirst(): number { + const num: number = this.peekFirst(); + // 队首指针向后移动一位 + this.front = this.index(this.front + 1); + this.queSize--; + return num; + } + + /* 队尾出队 */ + pollLast(): number { + const num: number = this.peekLast(); + this.queSize--; + return num; + } + + /* 访问队首元素 */ + peekFirst(): number { + 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."); + // 计算尾元素索引 + const last = this.index(this.front + this.queSize - 1); + return this.nums[last]; + } + + /* 返回数组用于打印 */ + toArray(): number[] { + // 仅转换有效长度范围内的列表元素 + const res: number[] = []; + for (let i = 0, j = this.front; i < this.queSize; i++, j++) { + res[i] = this.nums[this.index(j)]; + } + return res; + } + } ``` === "C"