tft每日頭條

 > 生活

 > leetcode鍊表經典題

leetcode鍊表經典題

生活 更新时间:2024-06-27 01:19:06
題目

設計你的循環隊列實現。

循環隊列是一種線性數據結構,其操作表現基于 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題目,考察循環隊列設計

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved