LeetCode 622:設計迴圈佇列 Design Circular Queue
LeetCode 622:設計迴圈佇列 Design Circular Queue
首先來看看佇列這種資料結構:
佇列:先入先出的資料結構
在 FIFO 資料結構中,將首先處理新增到佇列中的第一個元素
。
如上圖所示,佇列是典型的 FIFO 資料結構。插入(insert)操作也稱作入隊(enqueue),新元素始終被新增在佇列的末尾
。 刪除(delete)操作也被稱為出隊(dequeue)。 你只能移除第一個元素
。
佇列 - 實現
為了實現佇列,我們可以使用動態陣列和指向佇列頭部的索引。
如上所述,佇列應支援兩種操作:入隊和出隊。入隊會向佇列追加一個新元素,而出隊會刪除第一個元素。 所以我們需要一個索引來指出起點。
迴圈佇列
此前,我們提供了一種簡單但低效的佇列實現。
更有效的方法是使用迴圈佇列。 具體來說,我們可以使用固定大小的陣列
和兩個指標
來指示起始位置和結束位置。 目的是重用
我們之前提到的被浪費的儲存
。
讓我們通過一個示例來檢視迴圈佇列的工作原理。 你應該注意我們入隊
或出隊
元素時使用的策略。
[外鏈圖片轉存失敗(img-ScULOtla-1564547660610)(queue.gif)]
仔細檢查動畫,找出我們用來檢查佇列是空
還是滿
的策略。
以上內容來自力扣中國,內容有改變
題目:設計迴圈佇列:
設計你的迴圈佇列實現。 迴圈佇列是一種線性資料結構,其操作表現基於 FIFO(先進先出)原則並且隊尾被連線在隊首之後以形成一個迴圈。它也被稱為“環形緩衝器”。
迴圈佇列的一個好處是我們可以利用這個佇列之前用過的空間。在一個普通佇列裡,一旦一個佇列滿了,我們就不能插入下一個元素,即使在佇列前面仍有空間。但是使用迴圈佇列,我們能使用這些空間去儲存新的值。
你的實現應該支援如下操作:
MyCircularQueue(k)
: 構造器,設定佇列長度為 k 。Front
: 從隊首獲取元素。如果佇列為空,返回 -1 。Rear
: 獲取隊尾元素。如果佇列為空,返回 -1 。enQueue(value)
: 向迴圈佇列插入一個元素。如果成功插入則返回真。deQueue()
: 從迴圈佇列中刪除一個元素。如果成功刪除則返回真。isEmpty()
: 檢查迴圈佇列是否為空。isFull()
: 檢查迴圈佇列是否已滿。
示例:
MyCircularQueue circularQueue = new MycircularQueue(3); // 設定長度為 3
circularQueue.enQueue(1); // 返回 true
circularQueue.enQueue(2); // 返回 true
circularQueue.enQueue(3); // 返回 true
circularQueue.enQueue(4); // 返回 false,佇列已滿
circularQueue.Rear(); // 返回 3
circularQueue.isFull(); // 返回 true
circularQueue.deQueue(); // 返回 true
circularQueue.enQueue(4); // 返回 true
circularQueue.Rear(); // 返回 4
提示:
- 所有的值都在 0 至 1000 的範圍內;
- 運算元將在 1 至 1000 的範圍內;
- 請不要使用內建的佇列庫。
解題思路:
一般高階程式設計語言都會內建佇列庫,稍微參考一下即可。在迴圈佇列中,我們使用一個陣列
和兩個指標(head
和 tail
)。 head
表示佇列的起始位置,tail
表示佇列的結束位置。
class MyCircularQueue {
private int[] data;
private int head;
private int tail;
private int size;
/** 初始化資料結構,並規定佇列大小k */
public MyCircularQueue(int k) {
data = new int[k];
head = -1;
tail = -1;
size = k;
}
/** 在佇列插入一項,並返回插入是否成功 */
public boolean enQueue(int value) {
if (isFull() == true) {
return false;
}
if (isEmpty() == true) {
head = 0;
}
tail = (tail + 1) % size;
data[tail] = value;
return true;
}
/** 從佇列刪除一項,並返回刪除是否成功 */
public boolean deQueue() {
if (isEmpty() == true) {
return false;
}
if (head == tail) {
head = -1;
tail = -1;
return true;
}
head = (head + 1) % size;
return true;
}
/** 獲取佇列第一項 */
public int Front() {
if (isEmpty() == true) {
return -1;
}
return data[head];
}
/** 獲取佇列最後一項 */
public int Rear() {
if (isEmpty() == true) {
return -1;
}
return data[tail];
}
/** 檢查佇列是否為空 */
public boolean isEmpty() {
return head == -1;
}
/** 檢查佇列是否已滿 */
public boolean isFull() {
return ((tail + 1) % size) == head;
}
}
Python3:
class MyCircularQueue():
def __init__(self, k: int):
"""
初始化資料結構,並規定佇列大小k
"""
self.size = k
self.queue = ['']*k
self.head = -1
self.tail = -1
def enQueue(self, value: int) -> bool:
"""
在佇列插入一項,並返回插入是否成功
"""
if not self.isFull():
if self.head == -1:
self.head = 0
self.tail = (self.tail+1)%self.size
self.queue[self.tail] = value
return True
else:
return False
def deQueue(self) -> bool:
"""
從佇列刪除一項,並返回刪除是否成功
"""
if not self.isEmpty():
if self.head ==self.tail:
self.head,self.tail = -1,-1
else:
self.head = (self.head+1)%self.size
return True
else:
return False
def Front(self) -> int:
"""
獲取佇列第一項
"""
if self.isEmpty():
return -1
else:
return self.queue[self.head]
def Rear(self) -> int:
"""
獲取佇列最後一項
"""
if self.isEmpty():
return -1
else:
return self.queue[self.tail]
def isEmpty(self) -> bool:
"""
檢查佇列是否為空
"""
return self.head == -1 and self.tail == -1
def isFull(self) -> bool:
"""
檢查佇列是否已滿
"""
return (self.tail+1)%self.size ==self.head
相關推薦
LeetCode 622:設計迴圈佇列 Design Circular Queue
LeetCode 622:設計迴圈佇列 Design Circular Queue 首先來看看佇列這種資料結構: 佇列:先入先出的
[LeetCode] 622.Design Circular Queue 設計環形佇列 All LeetCode Questions List 題目彙總
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First
[LeetCode] 641.Design Circular Deque 設計環形雙向佇列 [LeetCode] 622.Design Circular Queue 設計環形佇列 All LeetCode Questions List 題目彙總
Design your implementation of the circular double-ended queue (deque). Your implementation should support following operations: MyCircularDeque(k): Const
[LeetCode] Design Circular Queue 設計環形佇列
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO
LeetCode 622——設計迴圈佇列
1. 題目 設計你的迴圈佇列實現。 迴圈佇列是一種線性資料結構,其操作表現基於 FIFO(先進先出)原則並且隊尾被連線在隊首之後以形成一個迴圈。它也被稱為“環形緩衝器”。 迴圈佇列的一個好處是我們可以利用這個佇列之前用過的空間。在一個普通佇列裡,一旦一個佇列滿了,我們就不能插入下
迴圈佇列實現 622.Design Circular Queue
import java.util.*; class MyCircularQueue { public int[] data; private int p_start; private int p_tail; private int k; priv
622. 設計迴圈佇列
622.設計迴圈佇列 設計你的迴圈佇列實現。 迴圈佇列是一種線性資料結構,其操作表現基於 FIFO(先進先出)原則並且隊尾被連線在隊首之後以形成一個迴圈。它也被稱為“環形緩衝器”。 迴圈佇列的一個好處是我們可以利用這個佇列之前用過的空間。在一個普通佇列裡,一旦一
佇列 & 棧//設計迴圈佇列
設計你的迴圈佇列實現。 迴圈佇列是一種線性資料結構,其操作表現基於 FIFO(先進先出)原則並且隊尾被連線在隊首之後以形成一個迴圈。它也被稱為“環形緩衝器”。 迴圈佇列的一個好處是我們可以利用這個佇列之前用過的空間。在一個普通佇列裡,一旦一個佇列滿了,我們就不能插入下一個元素,即使在佇列前面仍有空間
LeetCode622——設計迴圈佇列——c++根據不同STL容器的不同實現方式
最開始呢還是先貼出題面。 設計你的迴圈佇列實現。 迴圈佇列是一種線性資料結構,其操作表現基於 FIFO(先進先出)原則並且隊尾被連線在隊首之後以形成一個迴圈。它也被稱為“環形緩衝器”。 迴圈佇列的一個好處是我們可以利用這個佇列之前用過的空間。在一個普通佇列裡,一旦一個佇列
有n個人圍成一圈,從第1個人開始,1、2、3報數,報至3出局,餘下的人繼續從1、2、3報數,問:最後剩下的一人是原來的第幾號?同時求出被淘汰編號的序列。(要求:用迴圈佇列解決該問題。)
han.h: #include <iostream> #include <stdlib.h> #include "Status.h" using namespace std; typedef struct QNode {ElemType data;struct QNode *next;
演算法設計——迴圈佇列入隊出隊演算法
假設以不帶頭結點的迴圈連結串列表示佇列,並且只是一個指標指向隊尾結點。當不是頭指標。試設計相應的入隊和出隊演算法。 迴圈佇列入隊演算法 Enqueue template void EnqueucCNode *rear,T x) { s= new No
迴圈佇列的實現(Queue, C++版)
/* Queue.h */#ifndef __QUEUE_H__#define __QUEUE_H__ #include <iostream.h> extern "C" { void exit(int); } const int nDefaultQueueSize
[LeetCode] Design Circular Deque 設計環形雙向佇列
Design your implementation of the circular double-ended queue (deque). Your implementation should support following operations: M
14.設計迴圈序列-Leetcode 622(python)
題目描述 設計你的迴圈佇列實現。 迴圈佇列是一種線性資料結構,其操作表現基於 FIFO(先進先出)原則並且隊尾被連線在隊首之後以形成一個迴圈。它也被稱為“環形緩衝器”。 迴圈佇列的一個好處是我們可以利用這個佇列之前用過的空間。在一個普通佇列裡,一旦一個佇列滿了,我們就不能插入
資料結構實驗4:C++實現迴圈佇列
實驗4 4.1 實驗目的 熟練掌握佇列的順序儲存結構和鏈式儲存結構。 熟練掌握佇列的有關演算法設計,並在迴圈順序佇列和鏈佇列上實現。 根據具體給定的需求,合理設計並實現相關結構和演算法。 4.2 實驗要求 4.2.1 迴圈順序佇列的實驗要求 迴圈順序佇列結構和運算定義,演算法的實現以庫檔案方式實
資料結構實現(四):迴圈佇列(C++版)
資料結構實現(四):迴圈佇列(C++版) 1. 概念及基本框架 2. 基本操作程式實現 2.1 入隊操作 2.2 出隊操作 2.3 查詢操作 2.4 其他操作 3. 演算法複雜度分析 3.1 入
實驗二:迴圈佇列的驗證實驗 liujieying
檔案1:CirQueue.h 源程式: # ifndef CirQueue_H # define CirQueue_H const int QueueSize=100; template<class DataType> class CirQueue { public:
軟考:資料結構基礎——迴圈佇列C語言實現
迴圈佇列得實現: 1. 在入隊和出隊時,我們通過 q->rear = (q->rear +1)%MAX_LENTH 來實現迴圈入隊 q
C++——設計佇列類和迴圈佇列類
設計佇列類和迴圈佇列類 要求: 能夠設計佇列類和迴圈佇列類,實現儲存和取數功能。 Append:加入佇列,將一個元素加入到佇列的後面 Get:讀取佇列,從佇列前面讀取並刪除一個元素 IsEmpty:判斷佇列是否為空 IsFull:判斷佇列是否已滿 Traverse:遍歷,從頭至尾訪問佇列的每
Photoshop:6.14世界獻血海報設計分享poster design(內附psd)
前言 Photoshop:6.14世界獻血海報設計技巧分享poster design 作為一個ps新手,由於平臺比較少,能夠拿來鍛鍊的也就是學校的海報比賽,這個應該算是我第二次參加學校的海報大賽。這一次由於他的規則太科學,連尺寸都很隨意舉辦太隨意,令