設計實現雙端佇列。你的實現需要支援以下操作:
mycirculardeque(k):建構函式,雙端佇列的大小為k。
insertfront():將乙個元素新增到雙端佇列頭部。 如果操作成功返回 true。
insertlast():將乙個元素新增到雙端佇列尾部。如果操作成功返回 true。
deletefront():從雙端佇列頭部刪除乙個元素。 如果操作成功返回 true。
deletelast():從雙端佇列尾部刪除乙個元素。如果操作成功返回 true。
getfront():從雙端佇列頭部獲得乙個元素。如果雙端隊列為空,返回 -1。
getrear():獲得雙端佇列的最後乙個元素。 如果雙端隊列為空,返回 -1。
isempty():檢查雙端佇列是否為空。
isfull():檢查雙端佇列是否滿了。
1.deque
2.陣列實現
class mycirculardeque:
def __init__(self, k: int):
"""initialize your data structure here. set the size of the deque to be k.
"""self.head, self.tail = -1, -1
self.vec = [none for _ in range(k)]
def insertfront(self, value: int) -> bool:
"""adds an item at the front of deque. return true if the operation is successful.
"""if self.isfull(): return false
self.head -= 1
if self.head < 0:
self.head = len(self.vec)-1
self.vec[self.head] = value
return true
def insertlast(self, value: int) -> bool:
"""adds an item at the rear of deque. return true if the operation is successful.
"""if self.isfull():
return false
self.tail += 1
if self.tail == len(self.vec):
self.tail = 0
self.vec[self.tail] = value
return true
def deletefront(self) -> bool:
"""deletes an item from the front of deque. return true if the operation is successful.
"""if self.isempty():
return false
self.vec[self.head] = none
self.head += 1
return true
def deletelast(self) -> bool:
"""deletes an item from the rear of deque. return true if the operation is successful.
"""if self.isempty(): return false
self.vec[self.tail] = none
self.tail -= 1
return true
def getfront(self) -> int:
"""get the front item from the deque.
"""print(self.head, len(self.vec))
return self.vec[self.head]
def getrear(self) -> int:
"""get the last item from the deque.
"""return self.vec[self.tail]
def isempty(self) -> bool:
"""checks whether the circular deque is empty or not.
"""for x in self.vec:
if x != none:
return false
return true
def isfull(self) -> bool:
"""checks whether the circular deque is full or not.
"""for x in self.vec:
if x == none:
return false
return true
641 設計迴圈雙端佇列
2020 04 10 設計迴圈雙端佇列 設計實現雙端佇列。你的實現需要支援以下操作 mycirculardeque k 建構函式,雙端佇列的大小為k。insertfront 將乙個元素新增到雙端佇列頭部。如果操作成功返回 true。insertlast 將乙個元素新增到雙端佇列尾部。如果操作成功返回...
LeetCode 641 設計迴圈雙端佇列
設計實現雙端佇列。你的實現需要支援以下操作 示例 mycirculardeque circulardeque new mycirculardeque 3 設定容量大小為3 circulardeque.insertlast 1 返回 true circulardeque.insertlast 2 返回...
LeetCode 641 設計迴圈雙端佇列
設計實現雙端佇列。你的實現需要支援以下操作 mycirculardeque k 建構函式,雙端佇列的大小為k。insertfront 將乙個元素新增到雙端佇列頭部。如果操作成功返回 true。insertlast 將乙個元素新增到雙端佇列尾部。如果操作成功返回 true。deletefront 從雙...