首页
/
每日頭條
/
生活
/
leetcode鍊表經典題
leetcode鍊表經典題
更新时间:2026-01-30 06:15:51
題目

設計你的循環隊列實現。

循環隊列是一種線性數據結構,其操作表現基于 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 的範圍内;

請不要使用内置的隊列庫。

解題思路分析

1、切片;時間複雜度O(1),空間複雜度O(n)

leetcode鍊表經典題(leetcode622go設計循環隊列)1

type MyCircularQueue struct { queue []int k int } func Constructor(k int) MyCircularQueue { return MyCircularQueue{ queue: make([]int, 0), k: k, } } func (this *MyCircularQueue) EnQueue(value int) bool { if len(this.queue) == this.k { return false } this.queue = append(this.queue, value) return true } func (this *MyCircularQueue) DeQueue() bool { if len(this.queue) == 0 { return false } this.queue = this.queue[1:] return true } func (this *MyCircularQueue) Front() int { if len(this.queue) == 0 { return -1 } return this.queue[0] } func (this *MyCircularQueue) Rear() int { if len(this.queue) == 0 { return -1 } return this.queue[len(this.queue)-1] } func (this *MyCircularQueue) IsEmpty() bool { return len(this.queue) == 0 } func (this *MyCircularQueue) IsFull() bool { return len(this.queue) == this.k }

2、循環隊列;時間複雜度O(1),空間複雜度O(n)

type MyCircularQueue struct { queue []int k int front int // 隊首 rear int // 隊尾 } func Constructor(k int) MyCircularQueue { return MyCircularQueue{ queue: make([]int, k 1), k: k 1, front: 0, rear: 0, } } func (this *MyCircularQueue) EnQueue(value int) bool { if this.IsFull() { return false } // 隊尾入隊 this.queue[this.rear] = value this.rear if this.rear == this.k { this.rear = 0 } return true } func (this *MyCircularQueue) DeQueue() bool { if this.IsEmpty() { return false } // 隊尾出隊 this.front if this.front == this.k { this.front = 0 } return true } func (this *MyCircularQueue) Front() int { if this.IsEmpty() { return -1 } return this.queue[this.front] } func (this *MyCircularQueue) Rear() int { if this.IsEmpty() { return -1 } prev := this.rear - 1 if prev < 0 { prev = this.k - 1 } return this.queue[prev] } func (this *MyCircularQueue) IsEmpty() bool { return this.front == this.rear } func (this *MyCircularQueue) IsFull() bool { next := this.rear 1 if next == this.k { next = 0 } return next == this.front }

總結

Medium題目,考察循環隊列設計

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
翡翠如何劃分等級
翡翠如何劃分等級
說到翡翠,大家首先想到的應該都是綠色,其實翡翠還有很多顔色,那我們在購買時應怎樣選擇呢?在朋友眼中我是翡翠原石專家,一眼就能看出翡翠的真假,這些都是20幾年積累的經驗和技巧,我會定期在滕璞閣分享給大家,讓大家都快速、真正地了解翡翠原石,少走...
2026-01-30
喝大紅袍的禁忌有哪些
喝大紅袍的禁忌有哪些
喝大紅袍的禁忌有哪些?忌空腹飲用空腹飲用大紅袍,容易出現“茶醉”現象,主要表現為頭暈、惡心等症狀,而且空腹喝茶會對胃腸造成負擔,容易造成胃腸炎的發生,今天小編就來說說關于喝大紅袍的禁忌有哪些?下面更多詳細答案一起來看看吧!喝大紅袍的禁忌有哪...
2026-01-30
全民k歌單局最高分
全民k歌單局最高分
這兩天,小編在王者峽谷又被動若遊龍、“筆”若驚鴻的上官婉兒秀一臉,一套無敵連環斬直接達成三殺成就!不愧是王者榮耀天秀法師,令無數召喚師“避而遠之”的英雄。今天,小編為喜歡婉兒的召喚師們帶來了一則好消息:上官婉兒英雄主題曲《婉》已經上線,有“...
2026-01-30
杭州富陽萬達廣場
杭州富陽萬達廣場
杭州網記者郭鑫昨日,萬達集團衆望所歸拿下了富陽銀湖36号D、E地塊(富政儲出[2019]3号),富陽首宗“雙限”地塊成交。萬達一輪遊以總價330870.0萬元零溢價率競得,折合樓面價8689元/m2。這是萬達首次進入富陽市場,萬達集團的入駐...
2026-01-30
羊水跟喝水多有關系嗎
羊水跟喝水多有關系嗎
羊水跟喝水多有關系嗎?羊水量的多少與平時喝水沒有直接的關系,今天小編就來聊一聊關于羊水跟喝水多有關系嗎?接下來我們就一起去研究一下吧!羊水跟喝水多有關系嗎羊水量的多少與平時喝水沒有直接的關系。羊水的來源在妊娠早期主要來自母體血清經胎膜進入羊...
2026-01-30
Copyright 2023-2026 - www.tftnews.com All Rights Reserved