LeetCode 브러시 큐 주제

(641) 설계주기 양단 큐

설계 및 구현 양단 큐.
: 다음 작업의 구현을 지원해야
MyCircularDeque (K)를 : 생성자, 크기 양단 큐가 K 수 있습니다. insertFront () 양단 큐의 헤드에 요소를 추가.
작업이 성공하면 true를 돌려줍니다. insertLast () 양단 큐의 말미에 요소를 추가. 작업이 성공하면 true를 돌려줍니다.
deleteFront () : 양단 큐에서 헤더 요소를 삭제합니다. 작업이 성공하면 true를 돌려줍니다.
deleteLast는 () : 양단 큐 꼬리에서 요소를 제거합니다. 작업이 성공하면 true를 돌려줍니다.
getFront () : 양단 큐에서 헤더 요소를 얻을. 양단 큐가 비어 있으면, -1을 반환합니다. getRear는 ()하십시오 양단 큐의 마지막 요소를 얻을.
양단 큐가 비어 있으면, -1을 반환합니다. 그러나 IsEmpty () : 확인 양단 큐가 비어 있습니다. isFull는 () : 양단 큐가 가득 있는지 확인합니다. 예 :

MyCircularDeque circularDeque 새로운 MycircularDeque (3) = //는 용량의 크기를 설정 3.
circularDeque.insertLast (1이다.)에 해당하는 // 반환
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]의 범위에 대한 모든 값은 사용하지 않는 작동 범위 [1, 1000]의 개수를 내장 라이브러리 양단.

출처 : 숙박 버튼 (LeetCode)
링크 : HTTPS : //leetcode-cn.com/problems/design-circular-deque
난이도 : 중간이
모든 네트워크에서 공제 저작권. 상업 무단 전재 소스를 표시하시기 바랍니다 승인 된 공식, 비상업적 재판에 문의하시기 바랍니다.


생각이 디자인 주제에 내가 조금, 소위 덱과 첫 접촉을 의아해했다 시작. 그러나 문제에 대한 해결책은 내가 어떤 물건 원형 양단 큐라고 dalao에 응답 한 후 높은 평가에 대한 명확한 설명을 참조로 이동합니다.
단어를 요약하면 : 큐, 후면 및 전면 책임, 한 (즉, 관리 및 교체 아웃)에 대한 책임이 있습니다 만, 공유 된 책임은 후면 및 전면 양단 큐. 종단을 두 번 할 수있는주기를 설정하는 것이 중요하다라고 우리에게 이야기 큐 큐 조건에 따라, 나는 단지 앞을 운영 할 필요가와 (교체 아웃 관리 할 수 있습니다 둘 다 예) 공유의 후면. 그런 다음 빈 팀의 전체 조건 팀은 큐를 기반으로 설계 할 수있다.
여기서도 순차 저장 구조, 시간 후 저장 구조를 달성하기 위해 분산.
마지막으로 단순히 장점, 문제가 거의 모든 C ++ 높은 품질의 솔루션을 얻을 ++ C에, LeetCode 한숨을했다.

typedef struct MyCircularDeque 
{
    int front;
    int rear;
    int MAXSIZE;
    int data[];     //这是伸缩型数组成员,可以理解为动态数组,大小依据内存分配而定。不明白自行查阅
} MyCircularDeque;


bool myCircularDequeIsEmpty(MyCircularDeque* obj);
bool myCircularDequeIsFull(MyCircularDeque* obj);


/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque* myCircularDequeCreate(int k) {
    MyCircularDeque* obj = (MyCircularDeque*)malloc(sizeof(MyCircularDeque) + (k + 1) * sizeof(int));
    if(!obj)
        return NULL;
    memset(obj, 0, sizeof(MyCircularDeque) + (k + 1) * sizeof(int));
    obj->MAXSIZE = k + 1;	//多分配了一个位置是为了方便队满队空的判断.

    return obj;
}

/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool myCircularDequeInsertFront(MyCircularDeque* obj, int value) {
    if(myCircularDequeIsFull(obj))
        return false;

    obj->front = (obj->front - 1 + obj->MAXSIZE) % obj->MAXSIZE;
    obj->data[obj->front] = value;
    return true;
}

/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool myCircularDequeInsertLast(MyCircularDeque* obj, int value) {
    if(myCircularDequeIsFull(obj))
        return false;

    obj->data[obj->rear] = value;
    obj->rear = (obj->rear + 1) % obj->MAXSIZE;
    return true;
}

/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool myCircularDequeDeleteFront(MyCircularDeque* obj) {
    if(myCircularDequeIsEmpty(obj))
        return false;
    
    obj->front = (obj->front + 1) % obj->MAXSIZE;
    return true;
}

/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool myCircularDequeDeleteLast(MyCircularDeque* obj) {  //OVER
    if(myCircularDequeIsEmpty(obj))
        return false;

     obj->rear = (obj->rear - 1 + obj->MAXSIZE) % obj->MAXSIZE;
     return true;
}

/** Get the front item from the deque. */
int myCircularDequeGetFront(MyCircularDeque* obj) { //OVER
    return myCircularDequeIsEmpty(obj) ? -1 : obj->data[obj->front];
}

/** Get the last item from the deque. */
int myCircularDequeGetRear(MyCircularDeque* obj) {  //OVER
    if(myCircularDequeIsEmpty(obj))
        return -1;

    int index = (obj->rear - 1 +obj->MAXSIZE) % obj->MAXSIZE;
    return obj->data[index];
}

/** Checks whether the circular deque is empty or not. */
bool myCircularDequeIsEmpty(MyCircularDeque* obj) { //OVER
    return obj->rear == obj->front ? true : false;
}

/** Checks whether the circular deque is full or not. */
bool myCircularDequeIsFull(MyCircularDeque* obj) {  //OVER
    return (obj->rear +1) % obj->MAXSIZE == obj->front ? true : false;
}

void myCircularDequeFree(MyCircularDeque* obj) {    //OVER
    free(obj);
    obj = NULL;
}

622 설계주기 큐

설계 및 구현 양단 큐. 당신은 다음의 실현을 지원해야합니다

MyCircularDeque (K) : 생성자, 크기가 K 양단한다. insertFront () 양단 큐의 헤드에 요소를 추가.
작업이 성공하면 true를 돌려줍니다. insertLast () 양단 큐의 말미에 요소를 추가. 작업이 성공하면 true를 돌려줍니다.
deleteFront () : 양단 큐에서 헤더 요소를 삭제합니다. 작업이 성공하면 true를 돌려줍니다.
deleteLast는 () : 양단 큐 꼬리에서 요소를 제거합니다. 작업이 성공하면 true를 돌려줍니다.
getFront () : 양단 큐에서 헤더 요소를 얻을. 양단 큐가 비어 있으면, -1을 반환합니다. getRear는 ()하십시오 양단 큐의 마지막 요소를 얻을.
양단 큐가 비어 있으면, -1을 반환합니다. 그러나 IsEmpty () : 확인 양단 큐가 비어 있습니다. isFull는 () : 양단 큐가 가득 있는지 확인합니다. 예 :

MyCircularDeque circularDeque 새로운 MycircularDeque (3) = //는 용량의 크기를 설정 3.
circularDeque.insertLast (1이다.)에 해당하는 // 반환
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]의 범위에 대한 모든 값은 사용하지 않는 작동 범위 [1, 1000]의 개수를 내장 라이브러리 양단.

출처 : 숙박 버튼 (LeetCode)
난이도 : 중간
링크 : HTTPS는 : //leetcode-cn.com/problems/design-circular-deque
모든 네트워크에서 공제 저작권. 상업 무단 전재 소스를 표시하시기 바랍니다 승인 된 공식, 비상업적 재판에 문의하시기 바랍니다.

생각하지 않습니다 : 아무 말도. . .

typedef struct MyCircularDeque 
{
    int front;
    int rear;
    int MAXSIZE;
    int data[];     //这是伸缩型数组成员,可以理解为动态数组,大小依据内存分配而定。不明白自行查阅
} MyCircularDeque;


bool myCircularDequeIsEmpty(MyCircularDeque* obj);
bool myCircularDequeIsFull(MyCircularDeque* obj);


/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque* myCircularDequeCreate(int k) {
    MyCircularDeque* obj = (MyCircularDeque*)malloc(sizeof(MyCircularDeque) + (k + 1) * sizeof(int));
    if(!obj)
        return NULL;
    memset(obj, 0, sizeof(MyCircularDeque) + (k + 1) * sizeof(int));
    obj->MAXSIZE = k + 1;

    return obj;
}

/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool myCircularDequeInsertFront(MyCircularDeque* obj, int value) {
    if(myCircularDequeIsFull(obj))
        return false;

    obj->front = (obj->front - 1 + obj->MAXSIZE) % obj->MAXSIZE;
    obj->data[obj->front] = value;
    return true;
}

/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool myCircularDequeInsertLast(MyCircularDeque* obj, int value) {
    if(myCircularDequeIsFull(obj))
        return false;

    obj->data[obj->rear] = value;
    obj->rear = (obj->rear + 1) % obj->MAXSIZE;
    return true;
}

/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool myCircularDequeDeleteFront(MyCircularDeque* obj) {
    if(myCircularDequeIsEmpty(obj))
        return false;
    
    obj->front = (obj->front + 1) % obj->MAXSIZE;
    return true;
}

/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool myCircularDequeDeleteLast(MyCircularDeque* obj) {  //OVER
    if(myCircularDequeIsEmpty(obj))
        return false;

     obj->rear = (obj->rear - 1 + obj->MAXSIZE) % obj->MAXSIZE;
     return true;
}

/** Get the front item from the deque. */
int myCircularDequeGetFront(MyCircularDeque* obj) { //OVER
    return myCircularDequeIsEmpty(obj) ? -1 : obj->data[obj->front];
}

/** Get the last item from the deque. */
int myCircularDequeGetRear(MyCircularDeque* obj) {  //OVER
    if(myCircularDequeIsEmpty(obj))
        return -1;

    int index = (obj->rear - 1 +obj->MAXSIZE) % obj->MAXSIZE;
    return obj->data[index];
}

/** Checks whether the circular deque is empty or not. */
bool myCircularDequeIsEmpty(MyCircularDeque* obj) { //OVER
    return obj->rear == obj->front ? true : false;
}

/** Checks whether the circular deque is full or not. */
bool myCircularDequeIsFull(MyCircularDeque* obj) {  //OVER
    return (obj->rear +1) % obj->MAXSIZE == obj->front ? true : false;
}

void myCircularDequeFree(MyCircularDeque* obj) {    //OVER
    free(obj);
    obj = NULL;
}

요약 : 이 코호트 설계를위한 주요 고려 사항은 팀이 비워 것입니다, 전체 무엇 팀, 빈 및 전체 팀, 방법 후면 및 전면 포인터를하는 팀은 분명히있다. 이러한 조건을 고려, 우리는 큐가 설계하기 어려운 아니라고 생각합니다.

게시 19 개 원래 기사 · 원 찬양 2 · 조회수 2,538

추천

출처blog.csdn.net/SC_king/article/details/104905739