mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 23:56:29 +08:00
Update deque.js and deque.ts
This commit is contained in:
parent
1d1d13ec0e
commit
7d0d3df886
2 changed files with 8 additions and 8 deletions
|
@ -5,14 +5,14 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* 初始化双向队列 */
|
/* 初始化双向队列 */
|
||||||
/* JavaScript 没有内置的双端队列,可以把 Array 当作双端队列来使用 */
|
// JavaScript 没有内置的双端队列,只能把 Array 当作双端队列来使用
|
||||||
const deque = [];
|
const deque = [];
|
||||||
|
|
||||||
/* 元素入队 */
|
/* 元素入队 */
|
||||||
deque.push(2);
|
deque.push(2);
|
||||||
deque.push(5);
|
deque.push(5);
|
||||||
deque.push(4);
|
deque.push(4);
|
||||||
/* unshift() 方法的时间复杂度为 O(n) */
|
// 请注意,由于是数组,unshift() 方法的时间复杂度为 O(n)
|
||||||
deque.unshift(3);
|
deque.unshift(3);
|
||||||
deque.unshift(1);
|
deque.unshift(1);
|
||||||
console.log("双向队列 deque = ", deque);
|
console.log("双向队列 deque = ", deque);
|
||||||
|
@ -24,7 +24,7 @@ const peekLast = deque[deque.length - 1];
|
||||||
console.log("队尾元素 peekLast = " + peekLast);
|
console.log("队尾元素 peekLast = " + peekLast);
|
||||||
|
|
||||||
/* 元素出队 */
|
/* 元素出队 */
|
||||||
/* shift() 方法的时间复杂度为 O(n) */
|
// 请注意,由于是数组,shift() 方法的时间复杂度为 O(n)
|
||||||
const popFront = deque.shift();
|
const popFront = deque.shift();
|
||||||
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
|
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
|
||||||
const popBack = deque.pop();
|
const popBack = deque.pop();
|
||||||
|
|
|
@ -5,14 +5,14 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* 初始化双向队列 */
|
/* 初始化双向队列 */
|
||||||
/* JavaScript 没有内置的双端队列,可以把 Array 当作双端队列来使用 */
|
// TypeScript 没有内置的双端队列,只能把 Array 当作双端队列来使用
|
||||||
const deque: number[] = [];
|
const deque: number[] = [];
|
||||||
|
|
||||||
/* 元素入队 */
|
/* 元素入队 */
|
||||||
deque.push(2);
|
deque.push(2);
|
||||||
deque.push(5);
|
deque.push(5);
|
||||||
deque.push(4);
|
deque.push(4);
|
||||||
/* unshift() 方法的时间复杂度为 O(n) */
|
// 请注意,由于是数组,unshift() 方法的时间复杂度为 O(n)
|
||||||
deque.unshift(3);
|
deque.unshift(3);
|
||||||
deque.unshift(1);
|
deque.unshift(1);
|
||||||
console.log("双向队列 deque = ", deque);
|
console.log("双向队列 deque = ", deque);
|
||||||
|
@ -24,7 +24,7 @@ const peekLast: number = deque[deque.length - 1];
|
||||||
console.log("队尾元素 peekLast = " + peekLast);
|
console.log("队尾元素 peekLast = " + peekLast);
|
||||||
|
|
||||||
/* 元素出队 */
|
/* 元素出队 */
|
||||||
/* shift() 方法的时间复杂度为 O(n) */
|
// 请注意,由于是数组,shift() 方法的时间复杂度为 O(n)
|
||||||
const popFront: number = deque.shift() as number;
|
const popFront: number = deque.shift() as number;
|
||||||
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
|
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
|
||||||
const popBack: number = deque.pop() as number;
|
const popBack: number = deque.pop() as number;
|
||||||
|
|
Loading…
Reference in a new issue