Algorithm
設計循環(huán)雙端隊列
設計實現(xiàn)雙端隊列质礼。
你的實現(xiàn)需要支持以下操作:
MyCircularDeque(k):構造函數(shù),雙端隊列的大小為k。
insertFront():將一個元素添加到雙端隊列頭部沈贝。 如果操作成功返回 true。
insertLast():將一個元素添加到雙端隊列尾部勋乾。如果操作成功返回 true宋下。
deleteFront():從雙端隊列頭部刪除一個元素。 如果操作成功返回 true辑莫。
deleteLast():從雙端隊列尾部刪除一個元素学歧。如果操作成功返回 true。
getFront():從雙端隊列頭部獲得一個元素各吨。如果雙端隊列為空枝笨,返回 -1。
getRear():獲得雙端隊列的最后一個元素揭蜒。 如果雙端隊列為空横浑,返回 -1。
isEmpty():檢查雙端隊列是否為空屉更。
isFull():檢查雙端隊列是否滿了徙融。
示例:
MyCircularDeque circularDeque = new MycircularDeque(3); // 設置容量大小為3
circularDeque.insertLast(1); // 返回 true
circularDeque.insertLast(2); // 返回 true
circularDeque.insertFront(3); // 返回 true
circularDeque.insertFront(4); // 已經滿了,返回 false
circularDeque.getRear(); // 返回 2
circularDeque.isFull(); // 返回 true
circularDeque.deleteLast(); // 返回 true
circularDeque.insertFront(4); // 返回 true
circularDeque.getFront(); // 返回 4
提示:
所有值的范圍為 [1, 1000]
操作次數(shù)的范圍為 [1, 1000]
請不要使用內置的雙端隊列庫瑰谜。
class MyCircularDeque {
private ListNode<Integer> head;
private ListNode<Integer> tail;
private int size = 0;
private int number = 0;
/**
* Initialize your data structure here. Set the size of the deque to be k.
*/
public MyCircularDeque(int k) {
size = k;
}
/**
* Adds an item at the front of Deque. Return true if the operation is successful.
*/
public boolean insertFront(int value) {
if (number >= size) {
return false;
}
ListNode<Integer> node = new ListNode(value);
if (head != null) {
head.prev = node;
node.next = head;
head = node;
} else {
head = node;
tail = head;
}
number++;
return true;
}
/**
* Adds an item at the rear of Deque. Return true if the operation is successful.
*/
public boolean insertLast(int value) {
if (number >= size) {
return false;
}
ListNode<Integer> node = new ListNode(value);
if (tail != null) {
tail.next = node;
node.prev = tail;
tail = node;
} else {
head = node;
tail = head;
}
number++;
return true;
}
/**
* Deletes an item from the front of Deque. Return true if the operation is successful.
*/
public boolean deleteFront() {
if (number == 0) {
return false;
}
head = head.next;
if (head == null) {
tail = head;
}
if(number == 1){
head = null;
tail = null;
}
number--;
return true;
}
/**
* Deletes an item from the rear of Deque. Return true if the operation is successful.
*/
public boolean deleteLast() {
if (number == 0) {
return false;
}
tail = tail.prev;
if (tail == null) {
head = tail;
}
if(number == 1){
head = null;
tail = null;
}
number--;
return true;
}
/**
* Get the front item from the deque.
*/
public int getFront() {
if (number == 0) {
return -1;
}
return head.val;
}
/**
* Get the last item from the deque.
*/
public int getRear() {
if (number == 0) {
return -1;
}
return tail.val;
}
/**
* Checks whether the circular deque is empty or not.
*/
public boolean isEmpty() {
if (number == 0) {
return true;
}
return false;
}
/**
* Checks whether the circular deque is full or not.
*/
public boolean isFull() {
if (number == size) {
return true;
}
return false;
}
class ListNode<E> {
E val;
ListNode<E> next;
ListNode<E> prev;
ListNode(E element) {
this.val = element;
this.next = next;
}
}
}
Review
Tip
1欺冀、陳一一學校的選擇树绩,為了一家人在一起,我強加給孩子來面試深圳學校的壓力脚猾,智民雖然沒有考上葱峡,其他兩個學校都表現(xiàn)不錯,選擇中興小學龙助,雖然沒有商丘實驗學校好砰奕,勉強可以接受,來深圳后以后可以嘗試更多好學校的面試提鸟。
2军援、工作的選擇,決定從寶能離職到招銀網(wǎng)絡科技称勋,一定要和更優(yōu)秀的人一起工作胸哥,自己才能變得更優(yōu)秀,選擇精益和敏捷培訓方向對自己也是一個很大的挑戰(zhàn)赡鲜,要全力以赴空厌,當作自己的一份事業(yè)去實踐。
3银酬、老婆舍棄了剛有起色的工作嘲更,妥協(xié)來到深圳,這是很難抉擇的過程揩瞪,面對未來充滿著壓力和焦慮赋朦,我想我們一家人在一起后,我可以真正承擔起家的責任李破,分擔老婆的壓力宠哄,照顧好老婆。