mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 00:46:30 +08:00
feat: complement doc
This commit is contained in:
parent
d8bc3ba3e5
commit
77611bc758
1 changed files with 57 additions and 7 deletions
|
@ -21,7 +21,7 @@ comments: true
|
|||
<div class="center-table" markdown>
|
||||
|
||||
| 方法 | 描述 |
|
||||
| --------- | ------------------------ |
|
||||
| --------- | ---------------------------- |
|
||||
| offer() | 元素入队,即将元素添加至队尾 |
|
||||
| poll() | 队首元素出队 |
|
||||
| front() | 访问队首元素 |
|
||||
|
@ -143,13 +143,63 @@ comments: true
|
|||
=== "JavaScript"
|
||||
|
||||
```js title="queue.js"
|
||||
/* 初始化队列 */
|
||||
// Javascript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
// 注意:虽然Javascript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。
|
||||
const queue = [];
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue[0];
|
||||
|
||||
/* 元素出队 */
|
||||
const poll = queue.shift();
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.length;
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const empty = queue.length === 0;
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="queue.ts"
|
||||
/**
|
||||
* File: queue.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* 初始化队列 */
|
||||
// Typescript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
// 注意:虽然Typescript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。
|
||||
const queue: number[] = [];
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue[0];
|
||||
|
||||
/* 元素出队 */
|
||||
const poll = queue.shift();
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.length;
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const empty = queue.length === 0;
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
|
Loading…
Reference in a new issue