mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
Merge branch 'krahets:master' into typescript
This commit is contained in:
@@ -22,13 +22,13 @@ void merge(vector<int>& nums, int left, int mid, int right) {
|
||||
int i = leftStart, j = rightStart;
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for (int k = left; k <= right; k++) {
|
||||
// 若 “左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if (i > leftEnd)
|
||||
nums[k] = tmp[j++];
|
||||
// 否则,若 “右子数组已全部合并完” 或 “左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
// 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
else if (j > rightEnd || tmp[i] <= tmp[j])
|
||||
nums[k] = tmp[i++];
|
||||
// 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
// 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
else
|
||||
nums[k] = tmp[j++];
|
||||
}
|
||||
|
||||
@@ -59,19 +59,11 @@ public:
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek() {
|
||||
// 删除头结点
|
||||
if (empty())
|
||||
throw out_of_range("队列为空");
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 访问指定索引元素 */
|
||||
int get(int index) {
|
||||
if (index >= size())
|
||||
throw out_of_range("索引越界");
|
||||
return nums[(front + index) % capacity()];
|
||||
}
|
||||
|
||||
/* 将数组转化为 Vector 并返回 */
|
||||
vector<int> toVector() {
|
||||
int siz = size();
|
||||
@@ -104,11 +96,7 @@ int main() {
|
||||
/* 访问队首元素 */
|
||||
int peek = queue->peek();
|
||||
cout << "队首元素 peek = " << peek << endl;
|
||||
|
||||
/* 访问指定索引元素 */
|
||||
int num = queue->get(2);
|
||||
cout << "队列第 3 个元素为 num = " << num << endl;
|
||||
|
||||
|
||||
/* 元素出队 */
|
||||
int poll = queue->poll();
|
||||
cout << "出队元素 poll = " << poll << ",出队后 queue = ";
|
||||
|
||||
@@ -41,13 +41,6 @@ public:
|
||||
return stack.back();
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
int get(int index) {
|
||||
if(index >= size())
|
||||
throw out_of_range("索引越界");
|
||||
return stack[index];
|
||||
}
|
||||
|
||||
/* 返回 Vector */
|
||||
vector<int> toVector() {
|
||||
return stack;
|
||||
@@ -73,10 +66,6 @@ int main() {
|
||||
int top = stack->top();
|
||||
cout << "栈顶元素 top = " << top << endl;
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
int num = stack->get(3);
|
||||
cout << "栈索引 3 处的元素为 num = " << num << endl;
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack->pop();
|
||||
cout << "出栈元素 pop = " << pop << ",出栈后 stack = ";
|
||||
|
||||
@@ -21,15 +21,15 @@ func merge(nums []int, left, mid, right int) {
|
||||
i, j := left_start, right_start
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for k := left; k <= right; k++ {
|
||||
// 若 “左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if i > left_end {
|
||||
nums[k] = tmp[j]
|
||||
j++
|
||||
// 否则,若 “右子数组已全部合并完” 或 “左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
// 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
} else if j > right_end || tmp[i] <= tmp[j] {
|
||||
nums[k] = tmp[i]
|
||||
i++
|
||||
// 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
// 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
} else {
|
||||
nums[k] = tmp[j]
|
||||
j++
|
||||
|
||||
@@ -25,13 +25,13 @@ public class merge_sort {
|
||||
int i = leftStart, j = rightStart;
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for (int k = left; k <= right; k++) {
|
||||
// 若 “左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if (i > leftEnd)
|
||||
nums[k] = tmp[j++];
|
||||
// 否则,若 “右子数组已全部合并完” 或 “左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
// 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
else if (j > rightEnd || tmp[i] <= tmp[j])
|
||||
nums[k] = tmp[i++];
|
||||
// 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
// 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
else
|
||||
nums[k] = tmp[j++];
|
||||
}
|
||||
|
||||
@@ -58,19 +58,11 @@ class ArrayQueue {
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peek() {
|
||||
// 删除头结点
|
||||
if (isEmpty())
|
||||
throw new EmptyStackException();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
int get(int index) {
|
||||
if (index >= size())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return nums[(front + index) % capacity()];
|
||||
}
|
||||
|
||||
/* 返回数组 */
|
||||
public int[] toArray() {
|
||||
int size = size();
|
||||
|
||||
@@ -45,13 +45,6 @@ class ArrayStack {
|
||||
return stack.get(size() - 1);
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
public int get(int index) {
|
||||
if (index >= size())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stack.get(index);
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
public Object[] toArray() {
|
||||
return stack.toArray();
|
||||
@@ -75,10 +68,6 @@ public class array_stack {
|
||||
int peek = stack.peek();
|
||||
System.out.println("栈顶元素 peek = " + peek);
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
int num = stack.get(3);
|
||||
System.out.println("栈索引 3 处的元素为 num = " + num);
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack.pop();
|
||||
System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
@@ -20,13 +20,13 @@ function merge(nums, left, mid, right) {
|
||||
let i = leftStart, j = rightStart;
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for (let k = left; k <= right; k++) {
|
||||
// 若 “左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if (i > leftEnd) {
|
||||
nums[k] = tmp[j++];
|
||||
// 否则,若 “右子数组已全部合并完” 或 “左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
// 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
} else if (j > rightEnd || tmp[i] <= tmp[j]) {
|
||||
nums[k] = tmp[i++];
|
||||
// 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
// 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
} else {
|
||||
nums[k] = tmp[j++];
|
||||
}
|
||||
|
||||
110
codes/javascript/chapter_stack_and_queue/array_queue.js
Normal file
110
codes/javascript/chapter_stack_and_queue/array_queue.js
Normal file
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* File: array_queue.js
|
||||
* Created Time: 2022-12-13
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue {
|
||||
#queue; // 用于存储队列元素的数组
|
||||
#front = 0; // 头指针,指向队首
|
||||
#rear = 0; // 尾指针,指向队尾 + 1
|
||||
|
||||
constructor(capacity) {
|
||||
this.#queue = new Array(capacity);
|
||||
}
|
||||
|
||||
/* 获取队列的容量 */
|
||||
get capacity() {
|
||||
return this.#queue.length;
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
get size() {
|
||||
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
||||
return (this.capacity + this.#rear - this.#front) % this.capacity;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
empty() {
|
||||
return this.#rear - this.#front == 0;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
offer(num) {
|
||||
if (this.size == this.capacity)
|
||||
throw new Error("队列已满");
|
||||
// 尾结点后添加 num
|
||||
this.#queue[this.#rear] = num;
|
||||
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
||||
this.#rear = (this.#rear + 1) % this.capacity;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
poll() {
|
||||
const num = this.peek();
|
||||
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
||||
this.#front = (this.#front + 1) % this.capacity;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
peek() {
|
||||
if (this.empty())
|
||||
throw new Error("队列为空");
|
||||
return this.#queue[this.#front];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
const siz = this.size;
|
||||
const cap = this.capacity;
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
const arr = new Array(siz);
|
||||
for (let i = 0, j = this.#front; i < siz; i++, j++) {
|
||||
arr[i] = this.#queue[j % cap];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化队列 */
|
||||
const capacity = 10;
|
||||
const queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
console.log("队列 queue = ");
|
||||
console.log(queue.toArray());
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue.peek();
|
||||
console.log("队首元素 peek = " + peek);
|
||||
|
||||
/* 元素出队 */
|
||||
const poll = queue.poll();
|
||||
console.log("出队元素 poll = " + poll + ",出队后 queue = ");
|
||||
console.log(queue.toArray());
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.size;
|
||||
console.log("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const empty = queue.empty();
|
||||
console.log("队列是否为空 = " + empty);
|
||||
|
||||
/* 测试环形数组 */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
queue.offer(i);
|
||||
queue.poll();
|
||||
console.log("第 " + i + " 轮入队 + 出队后 queue = ");
|
||||
console.log(queue.toArray());
|
||||
}
|
||||
@@ -11,6 +11,7 @@ class ArrayStack {
|
||||
constructor() {
|
||||
this.stack = [];
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
get size() {
|
||||
return this.stack.length;
|
||||
@@ -28,22 +29,18 @@ class ArrayStack {
|
||||
|
||||
/* 出栈 */
|
||||
pop() {
|
||||
if (this.empty()) throw "栈为空";
|
||||
if (this.empty())
|
||||
throw new Error("栈为空");
|
||||
return this.stack.pop();
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
top() {
|
||||
if (this.empty()) throw "栈为空";
|
||||
if (this.empty())
|
||||
throw new Error("栈为空");
|
||||
return this.stack[this.stack.length - 1];
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
get(index) {
|
||||
if (index >= this.size) throw "索引越界";
|
||||
return this.stack[index];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
return this.stack;
|
||||
@@ -69,10 +66,6 @@ console.log(stack.toArray());
|
||||
const top = stack.top();
|
||||
console.log("栈顶元素 top = " + top);
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
const num = stack.get(3);
|
||||
console.log("栈索引 3 处的元素为 num = " + num);
|
||||
|
||||
/* 元素出栈 */
|
||||
const pop = stack.pop();
|
||||
console.log("出栈元素 pop = " + pop + ",出栈后 stack = ");
|
||||
|
||||
102
codes/javascript/chapter_stack_and_queue/linkedlist_queue.js
Normal file
102
codes/javascript/chapter_stack_and_queue/linkedlist_queue.js
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* File: linkedlist_queue.js
|
||||
* Created Time: 2022-12-20
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
const ListNode = require("../include/ListNode");
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
#front; // 头结点 #front
|
||||
#rear; // 尾结点 #rear
|
||||
#queSize = 0;
|
||||
|
||||
constructor() {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
get size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
isEmpty() {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
offer(num) {
|
||||
// 尾结点后添加 num
|
||||
const node = new ListNode(num);
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
if (!this.#front) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
// 如果队列不为空,则将该结点添加到尾结点后
|
||||
} else {
|
||||
this.#rear.next = node;
|
||||
this.#rear = node;
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
poll() {
|
||||
const num = this.peek();
|
||||
// 删除头结点
|
||||
this.#front = this.#front.next;
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
peek() {
|
||||
if (this.size === 0)
|
||||
throw new Error("队列为空");
|
||||
return this.#front.val;
|
||||
}
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
toArray() {
|
||||
let node = this.#front;
|
||||
const res = new Array(this.size);
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化队列 */
|
||||
const queue = new LinkedListQueue();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
console.log("队列 queue = " + queue.toArray());
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue.peek();
|
||||
console.log("队首元素 peek = " + peek);
|
||||
|
||||
/* 元素出队 */
|
||||
const poll = queue.poll();
|
||||
console.log("出队元素 poll = " + poll + ",出队后 queue = " + queue.toArray());
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.size;
|
||||
console.log("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const isEmpty = queue.isEmpty();
|
||||
console.log("队列是否为空 = " + isEmpty);
|
||||
@@ -4,9 +4,10 @@
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化队列 */
|
||||
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
|
||||
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
const queue = [];
|
||||
|
||||
/* 元素入队 */
|
||||
@@ -20,7 +21,7 @@ queue.push(4);
|
||||
const peek = queue[0];
|
||||
|
||||
/* 元素出队 */
|
||||
// O(n)
|
||||
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
|
||||
const poll = queue.shift();
|
||||
|
||||
/* 获取队列的长度 */
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化栈 */
|
||||
// Javascript 没有内置的栈类,可以把 Array 当作栈来使用
|
||||
const stack = [];
|
||||
|
||||
@@ -24,15 +24,15 @@ def merge(nums, left, mid, right):
|
||||
i, j = left_start, right_start
|
||||
# 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for k in range(left, right + 1):
|
||||
# 若 “左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
# 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if i > left_end:
|
||||
nums[k] = tmp[j]
|
||||
j += 1
|
||||
# 否则,若 “右子数组已全部合并完” 或 “左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
# 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
elif j > right_end or tmp[i] <= tmp[j]:
|
||||
nums[k] = tmp[i]
|
||||
i += 1
|
||||
# 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
# 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
else:
|
||||
nums[k] = tmp[j]
|
||||
j += 1
|
||||
|
||||
@@ -42,7 +42,6 @@ class ArrayQueue:
|
||||
|
||||
""" 出队 """
|
||||
def poll(self):
|
||||
# 删除头结点
|
||||
num = self.peek()
|
||||
# 队头指针向后移动一位,若越过尾部则返回到数组头部
|
||||
self.__front = (self.__front + 1) % self.capacity()
|
||||
@@ -50,19 +49,11 @@ class ArrayQueue:
|
||||
|
||||
""" 访问队首元素 """
|
||||
def peek(self):
|
||||
# 删除头结点
|
||||
if self.is_empty():
|
||||
print("队列为空")
|
||||
return False
|
||||
return self.__nums[self.__front]
|
||||
|
||||
""" 访问指定位置元素 """
|
||||
def get(self, index):
|
||||
if index >= self.size():
|
||||
print("索引越界")
|
||||
return False
|
||||
return self.__nums[(self.__front + index) % self.capacity()]
|
||||
|
||||
""" 返回列表用于打印 """
|
||||
def to_list(self):
|
||||
res = [0] * self.size()
|
||||
@@ -90,10 +81,6 @@ if __name__ == "__main__":
|
||||
peek = queue.peek()
|
||||
print("队首元素 peek =", peek)
|
||||
|
||||
""" 访问索引 index 处元素 """
|
||||
num = queue.get(3)
|
||||
print("队列索引 3 处的元素为 num =", num)
|
||||
|
||||
""" 元素出队 """
|
||||
poll = queue.poll()
|
||||
print("出队元素 poll =", poll)
|
||||
|
||||
@@ -34,11 +34,6 @@ class ArrayStack:
|
||||
def peek(self):
|
||||
assert not self.is_empty(), "栈为空"
|
||||
return self.__stack[-1]
|
||||
|
||||
""" 访问索引 index 处元素 """
|
||||
def get(self, index):
|
||||
assert index < self.size(), "索引越界"
|
||||
return self.__stack[index]
|
||||
|
||||
""" 返回列表用于打印 """
|
||||
def to_list(self):
|
||||
@@ -62,10 +57,6 @@ if __name__ == "__main__":
|
||||
peek = stack.peek()
|
||||
print("栈顶元素 peek =", peek)
|
||||
|
||||
""" 访问索引 index 处元素 """
|
||||
num = stack.get(3)
|
||||
print("栈索引 3 处的元素为 num =", num)
|
||||
|
||||
""" 元素出栈 """
|
||||
pop = stack.pop()
|
||||
print("出栈元素 pop =", pop)
|
||||
|
||||
@@ -20,13 +20,13 @@ function merge(nums: number[], left: number, mid: number, right: number): void {
|
||||
let i = leftStart, j = rightStart;
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for (let k = left; k <= right; k++) {
|
||||
// 若 “左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if (i > leftEnd) {
|
||||
nums[k] = tmp[j++];
|
||||
// 否则,若 “右子数组已全部合并完” 或 “左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
// 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
} else if (j > rightEnd || tmp[i] <= tmp[j]) {
|
||||
nums[k] = tmp[i++];
|
||||
// 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
// 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
} else {
|
||||
nums[k] = tmp[j++];
|
||||
}
|
||||
|
||||
111
codes/typescript/chapter_stack_and_queue/array_queue.ts
Normal file
111
codes/typescript/chapter_stack_and_queue/array_queue.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* File: array_queue.ts
|
||||
* Created Time: 2022-12-11
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue {
|
||||
private queue: number[]; // 用于存储队列元素的数组
|
||||
private front: number = 0; // 头指针,指向队首
|
||||
private rear: number = 0; // 尾指针,指向队尾 + 1
|
||||
private CAPACITY: number = 1e5;
|
||||
|
||||
constructor(capacity?: number) {
|
||||
this.queue = new Array<number>(capacity ?? this.CAPACITY);
|
||||
}
|
||||
|
||||
/* 获取队列的容量 */
|
||||
get capacity(): number {
|
||||
return this.queue.length;
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
get size(): number {
|
||||
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
||||
return (this.capacity + this.rear - this.front) % this.capacity;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
empty(): boolean {
|
||||
return this.rear - this.front == 0;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
offer(num: number): void {
|
||||
if (this.size == this.capacity)
|
||||
throw new Error("队列已满");
|
||||
// 尾结点后添加 num
|
||||
this.queue[this.rear] = num;
|
||||
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
||||
this.rear = (this.rear + 1) % this.capacity;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
poll(): number {
|
||||
const num = this.peek();
|
||||
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
||||
this.front = (this.front + 1) % this.capacity;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
peek(): number {
|
||||
if (this.empty())
|
||||
throw new Error("队列为空");
|
||||
return this.queue[this.front];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray(): number[] {
|
||||
const siz = this.size;
|
||||
const cap = this.capacity;
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
const arr = new Array(siz);
|
||||
for (let i = 0, j = this.front; i < siz; i++, j++) {
|
||||
arr[i] = this.queue[j % cap];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
/* 初始化队列 */
|
||||
const capacity = 10;
|
||||
const queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
console.log("队列 queue = ");
|
||||
console.log(queue.toArray());
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue.peek();
|
||||
console.log("队首元素 peek = " + peek);
|
||||
|
||||
/* 元素出队 */
|
||||
const poll = queue.poll();
|
||||
console.log("出队元素 poll = " + poll + ",出队后 queue = ");
|
||||
console.log(queue.toArray());
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.size;
|
||||
console.log("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const empty = queue.empty();
|
||||
console.log("队列是否为空 = " + empty);
|
||||
|
||||
/* 测试环形数组 */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
queue.offer(i);
|
||||
queue.poll();
|
||||
console.log("第 " + i + " 轮入队 + 出队后 queue = ");
|
||||
console.log(queue.toArray());
|
||||
}
|
||||
|
||||
export { };
|
||||
@@ -29,22 +29,18 @@ class ArrayStack {
|
||||
|
||||
/* 出栈 */
|
||||
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];
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
get(index: number): number | undefined {
|
||||
if (index >= this.size) throw new Error('索引越界');
|
||||
return this.stack[index];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
return this.stack;
|
||||
@@ -70,10 +66,6 @@ console.log(stack.toArray());
|
||||
const top = stack.top();
|
||||
console.log("栈顶元素 top = " + top);
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
const num = stack.get(3);
|
||||
console.log("栈索引 3 处的元素为 num = " + num);
|
||||
|
||||
/* 元素出栈 */
|
||||
const pop = stack.pop();
|
||||
console.log("出栈元素 pop = " + pop + ",出栈后 stack = ");
|
||||
|
||||
102
codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts
Normal file
102
codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* File: linkedlist_queue.ts
|
||||
* Created Time: 2022-12-19
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
import ListNode from "../module/ListNode"
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
private front: ListNode | null; // 头结点 front
|
||||
private rear: ListNode | null; // 尾结点 rear
|
||||
private queSize: number = 0;
|
||||
|
||||
constructor() {
|
||||
this.front = null;
|
||||
this.rear = null;
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
get size(): number {
|
||||
return this.queSize;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
isEmpty(): boolean {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
offer(num: number): void {
|
||||
// 尾结点后添加 num
|
||||
const node = new ListNode(num);
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
if (!this.front) {
|
||||
this.front = node;
|
||||
this.rear = node;
|
||||
// 如果队列不为空,则将该结点添加到尾结点后
|
||||
} else {
|
||||
this.rear!.next = node;
|
||||
this.rear = node;
|
||||
}
|
||||
this.queSize++;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
poll(): number {
|
||||
const num = this.peek();
|
||||
if (!this.front)
|
||||
throw new Error("队列为空")
|
||||
// 删除头结点
|
||||
this.front = this.front.next;
|
||||
this.queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
peek(): number {
|
||||
if (this.size === 0)
|
||||
throw new Error("队列为空");
|
||||
return this.front!.val;
|
||||
}
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
toArray(): number[] {
|
||||
let node = this.front;
|
||||
const res = new Array<number>(this.size);
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
res[i] = node!.val;
|
||||
node = node!.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* 初始化队列 */
|
||||
const queue = new LinkedListQueue();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
console.log("队列 queue = " + queue.toArray());
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue.peek();
|
||||
console.log("队首元素 peek = " + peek);
|
||||
|
||||
/* 元素出队 */
|
||||
const poll = queue.poll();
|
||||
console.log("出队元素 poll = " + poll + ",出队后 queue = " + queue.toArray());
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.size;
|
||||
console.log("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const isEmpty = queue.isEmpty();
|
||||
console.log("队列是否为空 = " + isEmpty);
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
/* 初始化队列 */
|
||||
// TypeScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
|
||||
const queue: number[] = [];
|
||||
|
||||
/* 元素入队 */
|
||||
@@ -20,7 +19,7 @@ queue.push(4);
|
||||
const peek = queue[0];
|
||||
|
||||
/* 元素出队 */
|
||||
// O(n)
|
||||
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
|
||||
const poll = queue.shift();
|
||||
|
||||
/* 获取队列的长度 */
|
||||
|
||||
Reference in New Issue
Block a user