精華區beta Marginalman 關於我們 聯絡資訊
https://leetcode.com/problems/design-circular-deque/description 641. Design Circular Deque 設計一個雙邊佇列 思路: 1.照題目敘述設計,因為只關注前後所以用鏈結串列保存串列的頭尾就好。 java code ----------------------------------------------------------------- class MyCircularDeque { int k; int size; Node first; Node last; public MyCircularDeque(int k) { this.k = k; this.size = 0; } public boolean insertFront(int value) { if (isFull()) return false; Node node = new Node(value); if (isEmpty()) { last = first = node; } else { node.next = first; first.prev = node; first = node; } size++; return true; } public boolean insertLast(int value) { if (isFull()) return false; Node node = new Node(value); if (isEmpty()) { last = first = node; } else { last.next = node; node.prev = last; last = node; } size++; return true; } public boolean deleteFront() { if (isEmpty()) return false; size--; first = first.next; if (first != null) { first.prev = null; } return true; } public boolean deleteLast() { if (isEmpty()) return false; size--; last = last.prev; if (last != null) { last.next = null; } return true; } public int getFront() { if (isEmpty()) return -1; return first.val; } public int getRear() { if (isEmpty()) return -1; return last.val; } public boolean isEmpty() { return size == 0; } public boolean isFull() { return size == k; } } class Node { int val; Node prev; Node next; public Node(int val) { this.val = val; } } ----------------------------------------------------------------- -- 你跟我說這個我有什麼辦法 https://i.imgur.com/wb5zrOy.jpeg -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.158.191.3 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1727512533.A.6B1.html