641. Design Circular Deque
1 題目理解
要求設(shè)計(jì)一個(gè)雙端循環(huán)隊(duì)列。這個(gè)隊(duì)列能支持以下操作:
MyCircularDeque(k): Constructor, set the size of the deque to be k.
insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.
insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.
deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.
deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.
getFront(): Gets the front item from the Deque. If the deque is empty, return -1.
getRear(): Gets the last item from Deque. If the deque is empty, return -1.
isEmpty(): Checks whether Deque is empty or not.
isFull(): Checks whether Deque is full or not.
2 思路
再次來(lái)做的時(shí)候完全沒(méi)有想法,參考了答案后回過(guò)頭看自己的隊(duì)列文章發(fā)現(xiàn)思路就在文章的循環(huán)隊(duì)列里面講明白了。
重點(diǎn)理解:
1 front表示第一個(gè)數(shù)字有效的位置, tail表示最有一個(gè)有效數(shù)字位置的下一位,也可以理解為要插入一個(gè)元素時(shí)候的位置。這兩個(gè)變量的含義不能變。
2 隊(duì)列為空的條件:front =tail
3 隊(duì)列滿的條件:(tail+1)%capacity=0,是要空出一個(gè)位置的。
總結(jié)
以上是生活随笔為你收集整理的641. Design Circular Deque的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SpringBoot内嵌Tomcat原理
- 下一篇: LeetCode题解答案集合(完全版)