mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
build
This commit is contained in:
@@ -544,18 +544,22 @@ comments: true
|
||||
#front; // 头结点 #front
|
||||
#rear; // 尾结点 #rear
|
||||
#queSize = 0;
|
||||
|
||||
constructor() {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
get size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
isEmpty() {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
push(num) {
|
||||
// 尾结点后添加 num
|
||||
@@ -571,6 +575,7 @@ comments: true
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
poll() {
|
||||
const num = this.peek();
|
||||
@@ -579,12 +584,24 @@ comments: true
|
||||
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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -596,18 +613,22 @@ comments: true
|
||||
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;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
push(num: number): void {
|
||||
// 尾结点后添加 num
|
||||
@@ -623,6 +644,7 @@ comments: true
|
||||
}
|
||||
this.queSize++;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
poll(): number {
|
||||
const num = this.peek();
|
||||
@@ -633,12 +655,24 @@ comments: true
|
||||
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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1126,6 +1160,16 @@ comments: true
|
||||
throw new Error("队列为空");
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
const arr = new Array(this.size);
|
||||
for (let i = 0, j = this.#front; i < this.size; i++, j++) {
|
||||
arr[i] = this.#nums[j % this.capacity];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1187,6 +1231,16 @@ comments: true
|
||||
throw new Error("队列为空");
|
||||
return this.nums[this.front];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray(): number[] {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
const arr = new Array(this.size);
|
||||
for (let i = 0, j = this.front; i < this.size; i++, j++) {
|
||||
arr[i] = this.nums[j % this.capacity];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -513,23 +513,23 @@ comments: true
|
||||
```javascript title="linkedlist_stack.js"
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack {
|
||||
#stackPeek; // 将头结点作为栈顶
|
||||
#stackPeek; // 将头结点作为栈顶
|
||||
#stkSize = 0; // 栈的长度
|
||||
|
||||
|
||||
constructor() {
|
||||
this.#stackPeek = null;
|
||||
}
|
||||
|
||||
|
||||
/* 获取栈的长度 */
|
||||
get size() {
|
||||
return this.#stkSize;
|
||||
}
|
||||
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
isEmpty() {
|
||||
return this.size == 0;
|
||||
}
|
||||
|
||||
|
||||
/* 入栈 */
|
||||
push(num) {
|
||||
const node = new ListNode(num);
|
||||
@@ -537,26 +537,22 @@ comments: true
|
||||
this.#stackPeek = node;
|
||||
this.#stkSize++;
|
||||
}
|
||||
|
||||
|
||||
/* 出栈 */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
if (!this.#stackPeek) {
|
||||
throw new Error("栈为空!");
|
||||
}
|
||||
this.#stackPeek = this.#stackPeek.next;
|
||||
this.#stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
peek() {
|
||||
if (!this.#stackPeek) {
|
||||
throw new Error("栈为空!");
|
||||
}
|
||||
if (!this.#stackPeek)
|
||||
throw new Error("栈为空");
|
||||
return this.#stackPeek.val;
|
||||
}
|
||||
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
toArray() {
|
||||
let node = this.#stackPeek;
|
||||
@@ -576,22 +572,22 @@ comments: true
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack {
|
||||
private stackPeek: ListNode | null; // 将头结点作为栈顶
|
||||
private stkSize: number = 0; // 栈的长度
|
||||
|
||||
private stkSize: number = 0; // 栈的长度
|
||||
|
||||
constructor() {
|
||||
this.stackPeek = null;
|
||||
}
|
||||
|
||||
|
||||
/* 获取栈的长度 */
|
||||
get size(): number {
|
||||
return this.stkSize;
|
||||
}
|
||||
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
isEmpty(): boolean {
|
||||
return this.size == 0;
|
||||
}
|
||||
|
||||
|
||||
/* 入栈 */
|
||||
push(num: number): void {
|
||||
const node = new ListNode(num);
|
||||
@@ -599,26 +595,24 @@ comments: true
|
||||
this.stackPeek = node;
|
||||
this.stkSize++;
|
||||
}
|
||||
|
||||
|
||||
/* 出栈 */
|
||||
pop(): number {
|
||||
const num = this.peek();
|
||||
if (!this.stackPeek) {
|
||||
throw new Error("栈为空!");
|
||||
}
|
||||
if (!this.stackPeek)
|
||||
throw new Error("栈为空");
|
||||
this.stackPeek = this.stackPeek.next;
|
||||
this.stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
peek(): number {
|
||||
if (!this.stackPeek) {
|
||||
throw new Error("栈为空!");
|
||||
}
|
||||
if (!this.stackPeek)
|
||||
throw new Error("栈为空");
|
||||
return this.stackPeek.val;
|
||||
}
|
||||
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
toArray(): number[] {
|
||||
let node = this.stackPeek;
|
||||
@@ -941,30 +935,40 @@ comments: true
|
||||
constructor() {
|
||||
this.stack = [];
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
get size() {
|
||||
return this.stack.length;
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
empty() {
|
||||
return this.stack.length === 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
push(num) {
|
||||
this.stack.push(num);
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
pop() {
|
||||
if (this.empty())
|
||||
throw new Error("栈为空");
|
||||
return this.stack.pop();
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
top() {
|
||||
if (this.empty())
|
||||
throw new Error("栈为空");
|
||||
return this.stack[this.stack.length - 1];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
return this.stack;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
@@ -977,30 +981,40 @@ comments: true
|
||||
constructor() {
|
||||
this.stack = [];
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
get size(): number {
|
||||
return this.stack.length;
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
empty(): boolean {
|
||||
return this.stack.length === 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
push(num: number): void {
|
||||
this.stack.push(num);
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
pop(): number | undefined {
|
||||
if (this.empty())
|
||||
throw new Error('栈为空');
|
||||
return this.stack.pop();
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
top(): number | undefined {
|
||||
if (this.empty())
|
||||
throw new Error('栈为空');
|
||||
return this.stack[this.stack.length - 1];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
return this.stack;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user