본문 바로가기
프로그래밍/LeetCode

C++ 641. Design Circular Deque(Leet Code)

by devsu 2020. 7. 22.

Leet Code_641. Design Circular Deque

 

Circular Deque

 

https://leetcode.com/problems/design-circular-deque/

 

Design Circular Deque - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

문제 해석

 

이 문제를 풀기위해 이해해야 할 내용은 다음과 같습니다.

 

목표

Circular Dequeue 만들기

방법

Circular Dequeue 만들기

 

결과

Circular Dequeue 만들기

 

통과한 코드

class MyCircularDeque {
public:
    vector<int> v;
    int nSize;
    /** Initialize your data structure here. Set the size of the deque to be k. */
    MyCircularDeque(int k) {
        nSize = k;
    }
    
    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    bool insertFront(int value) {
        if(!isFull())
        {
            v.insert(v.begin(), value);
            return true;
        }
        return false;
    }
    
    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    bool insertLast(int value) {
        if(!isFull())
        {
           v.push_back(value);
            return true;
        }
        return false;
    }
    
    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    bool deleteFront() {
        if(!isEmpty())
        {
            v.erase(v.begin());
            return true;
        }
        return false;
    }
    
    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    bool deleteLast() {
        if(!isEmpty())
        {
            v.pop_back();
            return true;
        }
        return false;
    }
    
    /** Get the front item from the deque. */
    int getFront() {
        if(isEmpty())
            return -1;
        else
            return v.front();
    }
    
    /** Get the last item from the deque. */
    int getRear() {
        if(isEmpty())
            return -1;
        else
            return v.back();
    }
    
    /** Checks whether the circular deque is empty or not. */
    bool isEmpty() {
        if(v.size() == 0)
            return true;
        else
            return false;
    }
    
    /** Checks whether the circular deque is full or not. */
    bool isFull() {
        if(nSize == v.size())
            return true;
        else
            return false;
    }
};

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque* obj = new MyCircularDeque(k);
 * bool param_1 = obj->insertFront(value);
 * bool param_2 = obj->insertLast(value);
 * bool param_3 = obj->deleteFront();
 * bool param_4 = obj->deleteLast();
 * int param_5 = obj->getFront();
 * int param_6 = obj->getRear();
 * bool param_7 = obj->isEmpty();
 * bool param_8 = obj->isFull();
 */

 

Vector를 이용하여 함수를 사용하여 구현