mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 22:28:40 +08:00 
			
		
		
		
	Update queue.ts
This commit is contained in:
		@ -5,8 +5,8 @@
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 初始化队列 */
 | 
					/* 初始化队列 */
 | 
				
			||||||
// Typescript 没有内置的队列,可以把 Array 当作队列来使用 
 | 
					// TypeScript 没有内置的队列,可以把 Array 当作队列来使用 
 | 
				
			||||||
// 注意:虽然Typescript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。
 | 
					// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
 | 
				
			||||||
const queue: number[] = [];
 | 
					const queue: number[] = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 元素入队 */
 | 
					/* 元素入队 */
 | 
				
			||||||
@ -20,6 +20,7 @@ queue.push(4);
 | 
				
			|||||||
const peek = queue[0];
 | 
					const peek = queue[0];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 元素出队 */
 | 
					/* 元素出队 */
 | 
				
			||||||
 | 
					// O(n)
 | 
				
			||||||
const poll = queue.shift();
 | 
					const poll = queue.shift();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 获取队列的长度 */
 | 
					/* 获取队列的长度 */
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user