LeetCode 641. Design Circular Deque

原题链接在这里:https://leetcode.com/problems/design-circular-deque/

题目:

Design your implementation of the circular double-ended queue (deque).

Your implementation should support following operations:

  • 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.

Example:

MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3
circularDeque.insertLast(1);			// return true
circularDeque.insertLast(2);			// return true
circularDeque.insertFront(3);			// return true
circularDeque.insertFront(4);			// return false, the queue is full
circularDeque.getRear();  			// return 2
circularDeque.isFull();				// return true
circularDeque.deleteLast();			// return true
circularDeque.insertFront(4);			// return true
circularDeque.getFront();			// return 4

Note:

  • All values will be in the range of [0, 1000].
  • The number of operations will be in the range of [1, 1000].
  • Please do not use the built-in Deque library.

题解:

Use an array to maintain values.

start index pointing to queue head, initialized as -1.

end index pointing to queue tail, initialized as -1.

When insertFront, if start == -1, assign start as 0, else start = (start - 1 + k) % k. Assign new value to arr[start]. If end is -1, need to update end = start. This only happens at the beginning.

When insertLast, end = (end + 1) % k. Assign new value to arr[end]. If start is -1, need to update start = end. This only happends at the begining.

deleteFront, move start + 1.

deleteLast, move end - 1.

Time Complexity: MyCircularDeque, O(1). insertFront, O(1). insertLast, O(1). deleteFront, O(1). deleteLast, O(1). getFront, O(1). getRear, O(1). isEmpty, O(1). isEmpty, O(1).

Space: O(k).

AC Java:

  1 class MyCircularDeque {
  2     int [] arr;
  3     int start;
  4     int end;
  5     int len;
  6     int k;
  7
  8     /** Initialize your data structure here. Set the size of the deque to be k. */
  9     public MyCircularDeque(int k) {
 10         arr = new int[k];
 11         start = -1;
 12         end = -1;
 13         len = 0;
 14         this.k = k;
 15     }
 16
 17     /** Adds an item at the front of Deque. Return true if the operation is successful. */
 18     public boolean insertFront(int value) {
 19         if(isFull()){
 20             return false;
 21         }
 22
 23         if(start == -1){
 24             start = 0;
 25         }else{
 26             start = (start - 1 + k) % k;
 27         }
 28
 29         arr[start] = value;
 30         if(end == -1){
 31             end = start;
 32         }
 33
 34         len++;
 35         return true;
 36     }
 37
 38     /** Adds an item at the rear of Deque. Return true if the operation is successful. */
 39     public boolean insertLast(int value) {
 40         if(isFull()){
 41             return false;
 42         }
 43
 44         end = (end + 1) % k;
 45         arr[end] = value;
 46         if(start == -1){
 47             start = end;
 48         }
 49
 50         len++;
 51         return true;
 52     }
 53
 54     /** Deletes an item from the front of Deque. Return true if the operation is successful. */
 55     public boolean deleteFront() {
 56         if(isEmpty()){
 57             return false;
 58         }
 59
 60         start = (start + 1) % k;
 61         len--;
 62         return true;
 63     }
 64
 65     /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
 66     public boolean deleteLast() {
 67         if(isEmpty()){
 68             return false;
 69         }
 70
 71         end = (end - 1 + k) % k;
 72         len--;
 73         return true;
 74     }
 75
 76     /** Get the front item from the deque. */
 77     public int getFront() {
 78         return isEmpty() ? -1 : arr[start];
 79     }
 80
 81     /** Get the last item from the deque. */
 82     public int getRear() {
 83         return isEmpty() ? -1 : arr[end];
 84     }
 85
 86     /** Checks whether the circular deque is empty or not. */
 87     public boolean isEmpty() {
 88         return len == 0;
 89     }
 90
 91     /** Checks whether the circular deque is full or not. */
 92     public boolean isFull() {
 93         return len == k;
 94     }
 95 }
 96
 97 /**
 98  * Your MyCircularDeque object will be instantiated and called as such:
 99  * MyCircularDeque obj = new MyCircularDeque(k);
100  * boolean param_1 = obj.insertFront(value);
101  * boolean param_2 = obj.insertLast(value);
102  * boolean param_3 = obj.deleteFront();
103  * boolean param_4 = obj.deleteLast();
104  * int param_5 = obj.getFront();
105  * int param_6 = obj.getRear();
106  * boolean param_7 = obj.isEmpty();
107  * boolean param_8 = obj.isFull();
108  */

类似Design Circular Queue.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12079440.html

时间: 2024-11-09 03:58:44

LeetCode 641. Design Circular Deque的相关文章

LeetCode 622. Design Circular Queue

原题链接在这里:https://leetcode.com/problems/design-circular-queue/ 题目: Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and th

Design Circular Deque

Design your implementation of the circular double-ended queue (deque). Your implementation should support following operations: MyCircularDeque(k): Constructor, set the size of the deque to be k. insertFront(): Adds an item at the front of Deque. Ret

[LeetCode] Design Circular Queue 设计环形队列

Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a

LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)

Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext. The given compressed string will be in the form of each letter followed by a positive integer representing the numbe

leetcode 355 Design Twitte

题目连接 https://leetcode.com/problems/design-twitter Design Twitte Description Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your de

[LeetCode] 534. Design TinyURL 设计短网址

Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design a URL shortening service that is similar to TinyURL? Background:TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com

[LeetCode] 355. Design Twitter 设计推特

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods: postTweet(userId, tweetId): Compo

LeetCode 642. Design Search Autocomplete System

原题链接在这里:https://leetcode.com/problems/design-search-autocomplete-system/ 题目: Design a search autocomplete system for a search engine. Users may input a sentence (at least one word and end with a special character '#'). For each character they type ex

LeetCode 706. Design HashMap

原题链接在这里:https://leetcode.com/problems/design-hashmap/ 题目: Design a HashMap without using any built-in hash table libraries. To be specific, your design should include these functions: put(key, value) : Insert a (key, value) pair into the HashMap. If