ARTS第八周20200712

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

分布式系統(tǒng)工程師的分布式系統(tǒng)理論

Tip

1欺冀、陳一一學校的選擇树绩,為了一家人在一起,我強加給孩子來面試深圳學校的壓力脚猾,智民雖然沒有考上葱峡,其他兩個學校都表現(xiàn)不錯,選擇中興小學龙助,雖然沒有商丘實驗學校好砰奕,勉強可以接受,來深圳后以后可以嘗試更多好學校的面試提鸟。
2军援、工作的選擇,決定從寶能離職到招銀網(wǎng)絡科技称勋,一定要和更優(yōu)秀的人一起工作胸哥,自己才能變得更優(yōu)秀,選擇精益和敏捷培訓方向對自己也是一個很大的挑戰(zhàn)赡鲜,要全力以赴空厌,當作自己的一份事業(yè)去實踐。
3银酬、老婆舍棄了剛有起色的工作嘲更,妥協(xié)來到深圳,這是很難抉擇的過程揩瞪,面對未來充滿著壓力和焦慮赋朦,我想我們一家人在一起后,我可以真正承擔起家的責任李破,分擔老婆的壓力宠哄,照顧好老婆。

Share

MEGAEASE的遠程工作文化

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末嗤攻,一起剝皮案震驚了整個濱河市毛嫉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌妇菱,老刑警劉巖狱庇,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異恶耽,居然都是意外死亡密任,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門偷俭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浪讳,“玉大人,你說我怎么就攤上這事涌萤⊙妥瘢” “怎么了口猜?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長透揣。 經常有香客問我济炎,道長,這世上最難降的妖魔是什么辐真? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任须尚,我火速辦了婚禮,結果婚禮上侍咱,老公的妹妹穿的比我還像新娘耐床。我一直安慰自己,他們只是感情好楔脯,可當我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布撩轰。 她就那樣靜靜地躺著,像睡著了一般昧廷。 火紅的嫁衣襯著肌膚如雪堪嫂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天木柬,我揣著相機與錄音皆串,去河邊找鬼。 笑死弄诲,一個胖子當著我的面吹牛,可吹牛的內容都是我干的娇唯。 我是一名探鬼主播齐遵,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼塔插!你這毒婦竟也來了梗摇?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤想许,失蹤者是張志新(化名)和其女友劉穎伶授,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體流纹,經...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡糜烹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了漱凝。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片疮蹦。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖茸炒,靈堂內的尸體忽然破棺而出愕乎,到底是詐尸還是另有隱情阵苇,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布感论,位于F島的核電站绅项,受9級特大地震影響,放射性物質發(fā)生泄漏比肄。R本人自食惡果不足惜快耿,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望薪前。 院中可真熱鬧润努,春花似錦、人聲如沸示括。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽垛膝。三九已至鳍侣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間吼拥,已是汗流浹背倚聚。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留凿可,地道東北人惑折。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像枯跑,于是被迫代替她去往敵國和親惨驶。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,960評論 2 355