1.?有效的括號 - 力扣(LeetCode)
? 第一題判斷有效的括號,這道題我們會用到棧的知識,棧是后進先出的,可以根據這個來解這道題,先看一下題目和示例。
1.1整體思路?
我們通過示例可以看出括號匹配就返回true,不匹配就返回false,這里的思路就是使用棧,如果是左括號,我們就入棧,如果是右括號,我們就取棧頂元素和右括號進行比較,如果匹配就比較下一個,不匹配直接返回false,我們可以結合圖例來理解一下!
?以上是一些常見的例子,但是也有一些特殊的情況
1.']'只有一個右括號時,這個時候我們就需要加一些條件,如果棧頂沒有元素,證明沒有左括號入棧,那么也就是說沒有左括號和右括號匹配,那就直接返回false。
2.'['只有一個括號時,那么也就是說只有左括號入棧,然后就直接返回了,這里我們可以用判斷一下棧是否為空,用bool來返回,如果為空,返回true,如果不是空,證明棧里還有左括號,那也說明左括號和右擴號的數量不匹配,這樣就可以有效解決了
1.2代碼分析?
因為還沒有學C++,所以這里的棧都是自己模擬實現的
1.3整體代碼
typedef char STDatatype;typedef struct Stack
{STDatatype* a;int top;int capacity;
}ST;//函數聲明
//函數的初始化和銷毀
void STInit(ST* pst);
void STDestroy(ST* pst);//進棧和出棧
void STPush(ST* pst, STDatatype x);
void STPop(ST* pst);//棧頂
STDatatype STTop(ST* pst);//棧是否為空
bool STEmpty(ST* pst);//個數
int STSize(ST* pst);
//函數的初始化和銷毀
void STInit(ST* pst)
{assert(pst);pst->a = NULL;pst->top = pst->capacity = 0;
}
void STDestroy(ST* pst)
{assert(pst);free(pst->a);pst->a = NULL;pst->top = pst->capacity = 0;
}
//進棧和出棧
void STPush(ST* pst, STDatatype x)
{assert(pst);if (pst->capacity == pst->top){//增加容量int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;STDatatype* tmp = (STDatatype*)realloc(pst->a,sizeof(STDatatype)*newcapacity);if (tmp == NULL){perror("realloc");exit(1);}pst->a = tmp;pst->capacity = newcapacity;}pst->a[pst->top] = x;pst->top++;
}
void STPop(ST* pst)
{assert(pst);assert(pst->top > 0);pst->top--;
}
//棧頂
STDatatype STTop(ST* pst)
{assert(pst);assert(pst->top > 0);return pst->a[pst->top - 1];
}
//棧是否為空
bool STEmpty(ST* pst)
{assert(pst);return pst->top == 0;//等于就是空,不等于0就是非空
}
//個數
int STSize(ST* pst)
{assert(pst);return pst->top;
}
bool isValid(char* s) {ST st;STInit(&st);while(*s != 0){//左括號入棧if(*s == '(' || *s == '[' || *s == '{'){STPush(&st,*s);}else{if(STEmpty(&st)){return false;}char top = STTop(&st);STPop(&st);if(top == '(' && *s != ')'|| top == '{' && *s != '}'|| top == '[' && *s != ']'){return false;}}++s;}bool ret = STEmpty(&st);return ret;
}
?2.?用隊列實現棧
這道題是讓我們用兩個隊列來實現一個棧,我們知道隊列是先進先出的,而棧是后進先出的,那我們要如何實現呢?
2.1整體思路:
這道題的思路就是我們把要插入的數據放到那個非空的隊列中,要保持一個隊列是空,一個隊列是非空,注意不可以兩個隊列都有數據,這樣我們會把自己繞進去!要刪除數據的時候,將非空隊列的最后一個數據留下來,將其他的插入到空的隊列當中,然后再將留下的那個數據刪除即可。畫圖來理解一下
?
2.2 代碼分析
2.3整體代碼?
//創建節點typedef int QDatatype;
typedef struct Queuenode
{QDatatype val;struct Queuenode* next;
}Qnode;//定義一個結構體存放指針
typedef struct Queue
{Qnode* phead;Qnode* ptail;int size;
}Queue;//隊列的初始化和銷毀
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);//隊列的插入和刪除
void QueuePush(Queue* pq, QDatatype x);
void QueuePop(Queue* pq);//取隊列頭和尾的值
QDatatype QueueFront(Queue* pq);
QDatatype QueueBank(Queue* pq);//是否為空
bool QueueEmpty(Queue* pq);//size
int QueueSize(Queue* pq);//隊列的初始化和銷毀
void QueueInit(Queue* pq)
{assert(pq);pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}
void QueueDestory(Queue* pq)
{assert(pq);Qnode* cur = pq->phead;while (cur){Qnode* next = cur->next;free(cur);cur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}//隊列的插入和刪除
void QueuePush(Queue* pq, QDatatype x)
{assert(pq);Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));if (newnode == NULL){perror("malloc:");exit(1);}//申請成功newnode->next = NULL;newnode->val = x;if (pq->ptail == NULL){pq->phead = pq->ptail = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}void QueuePop(Queue* pq)
{assert(pq);assert(pq->size != 0);//一個節點if (pq->phead == pq->ptail){free(pq->phead);pq->phead = pq->ptail = NULL;}else{//多個節點Qnode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}//取隊列頭和尾的值
QDatatype QueueFront(Queue* pq)
{assert(pq);assert(pq->phead);return pq->phead->val;
}
QDatatype QueueBank(Queue* pq)
{assert(pq);assert(pq->ptail);return pq->ptail->val;
}//是否為空
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->size == 0;
}//size
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}//匿名結構體
typedef struct {Queue q1;Queue q2;
} MyStack;MyStack* myStackCreate() {MyStack* obj =(MyStack*)malloc(sizeof(MyStack));QueueInit(&(obj->q1));QueueInit(&(obj->q2));return obj;
}void myStackPush(MyStack* obj, int x) {if(!QueueEmpty(&(obj->q1))){QueuePush(&(obj->q1),x);}else{QueuePush(&(obj->q2),x);}
}int myStackPop(MyStack* obj) {Queue* empty = &(obj->q1);Queue* noempty = &(obj->q2);if(!QueueEmpty(&(obj->q1))){empty = &(obj->q2);noempty = &(obj->q1);}while(QueueSize(noempty) > 1){QueuePush(empty,QueueFront(noempty));QueuePop(noempty);}int top = QueueFront(noempty);QueuePop(noempty);return top;
}int myStackTop(MyStack* obj) {if(!QueueEmpty(&(obj->q1))){return QueueBank(&(obj->q1));}else{return QueueBank(&(obj->q2));}
}bool myStackEmpty(MyStack* obj) {return QueueEmpty(&(obj->q1)) && QueueEmpty(&(obj->q2));
}void myStackFree(MyStack* obj) {QueueDestory(&(obj->q1));QueueDestory(&(obj->q2));free(obj);
}/*** Your MyStack struct will be instantiated and called as such:* MyStack* obj = myStackCreate();* myStackPush(obj, x);* int param_2 = myStackPop(obj);* int param_3 = myStackTop(obj);* bool param_4 = myStackEmpty(obj);* myStackFree(obj);
*/
?3.?用棧實現隊列
這道題是用兩個棧實現隊列,棧是后進先出,隊列是先進先出,這個就比較好實現了
3.1整體思路
我們可以建一個pushst和一個popst,一個用來插入數據,一個用來刪除數據,我們可以想一下將數據插入到pushst,棧是后進先出,那么我們將pushst的數據倒到popst,他們的順序就發生了改變,這個時候popst棧頂的數據也就是隊列第一個要出的數據,我們可以畫圖來看一下。這里還有一個小技巧,當popst不為空的時候,取棧頂就是隊列要出的數據,這里不用來回倒,插入數據直接插入pustst即可,當popst為空時把pushst的數據都倒入到popst棧中
3.2代碼分析?
3.3整體代碼?
typedef int STDatatype;typedef struct Stack
{STDatatype* a;int top;int capacity;
}ST;//函數聲明
//函數的初始化和銷毀
void STInit(ST* pst);
void STDestroy(ST* pst);//進棧和出棧
void STPush(ST* pst, STDatatype x);
void STPop(ST* pst);//棧頂
STDatatype STTop(ST* pst);//棧是否為空
bool STEmpty(ST* pst);//個數
int STSize(ST* pst);//函數的初始化和銷毀
void STInit(ST* pst)
{assert(pst);pst->a = NULL;pst->top = pst->capacity = 0;
}void STDestroy(ST* pst)
{assert(pst);free(pst->a);pst->a = NULL;pst->top = pst->capacity = 0;
}//進棧和出棧
void STPush(ST* pst, STDatatype x)
{assert(pst);if (pst->capacity == pst->top){//增加容量int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;STDatatype* tmp = (STDatatype*)realloc(pst->a,sizeof(STDatatype)*newcapacity);if (tmp == NULL){perror("realloc");exit(1);}pst->a = tmp;pst->capacity = newcapacity;}pst->a[pst->top] = x;pst->top++;
}
void STPop(ST* pst)
{assert(pst);assert(pst->top > 0);pst->top--;
}//棧頂
STDatatype STTop(ST* pst)
{assert(pst);assert(pst->top > 0);return pst->a[pst->top - 1];
}//棧是否為空
bool STEmpty(ST* pst)
{assert(pst);return pst->top == 0;//等于就是空,不等于0就是非空
}//個數
int STSize(ST* pst)
{assert(pst);return pst->top;
}typedef struct {ST pushst;ST popst;
} MyQueue;MyQueue* myQueueCreate() {MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));STInit(&obj->pushst);STInit(&obj->popst);return obj;
}void myQueuePush(MyQueue* obj, int x) {STPush(&obj->pushst,x);
}int myQueuePeek(MyQueue* obj) {if(STEmpty(&obj->popst)){while(!STEmpty(&obj->pushst)){STPush(&obj->popst,STTop(&obj->pushst));STPop(&obj->pushst);}}return STTop(&obj->popst);
}int myQueuePop(MyQueue* obj) {int f = myQueuePeek(obj);STPop(&obj->popst);return f;
}bool myQueueEmpty(MyQueue* obj) {return STEmpty(&obj->pushst) && STEmpty(&obj->popst);
}void myQueueFree(MyQueue* obj) {STDestroy(&obj->popst);STDestroy(&obj->pushst);free(obj);
}/*** Your MyQueue struct will be instantiated and called as such:* MyQueue* obj = myQueueCreate();* myQueuePush(obj, x);* int param_2 = myQueuePop(obj);* int param_3 = myQueuePeek(obj);* bool param_4 = myQueueEmpty(obj);* myQueueFree(obj);
*/
4.???????設計循環隊列
這道題是讓我們設計一個循環隊列,隊列是先進先出的,是一個循環,可以把他比較一個圖書館的桌子,有四個座位,也就是說最多可以坐四個人,如果超過四個人后面的人就要等待在坐的四個人有人坐,再坐進去,拿這個例子我們就可以很好的理解這個題目了!
4.1整體思路
這道題有多種解法,可以用鏈表來做,也可以用數組來做,在這里我還是用數組來做,鏈表比較麻煩,不論是在申請空間,還是找尾節點的前驅節點,大家感興趣可以拿鏈表做一下,這里就拿數組做了,拿數組做也有出現一些小問題,如果我們只申請k個空間,那么數組是空還是滿我們就分不清了,這里可以畫圖來看一下!
這里有兩種解法,一個是定義一個計數器,還有一個是多開一個空間,就是開k+1個空間,這樣我們就可以分清是空還是非空了,還有一個點就是如何讓他循環起來,就是使用%,也就是每次到尾%回到下標為0的元素,這里我就采用多開一個空間的方法了,也就是%(k+1)這里畫圖可以幫助大家理解一下?。
這里將把每一個函數都拿出來分析!
4.2代碼分析
4.2.1MyCircularQueue(k)
4.2.2isEmpty()
4.2.3isfull?
?4.2.4enQueue(value)和deQueue()
4.2.5Front和Rear
4.3整體代碼
typedef struct {int* a;int head;int tail;int k;
} MyCircularQueue;MyCircularQueue* myCircularQueueCreate(int k) {MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));obj->a = (int*)malloc(sizeof(int)*(k+1));obj->head = 0;obj->tail = 0;obj->k = k;return obj;
}bool myCircularQueueIsEmpty(MyCircularQueue* obj) {return obj->head == obj->tail;
}bool myCircularQueueIsFull(MyCircularQueue* obj) {return (obj->tail+1) % (obj->k+1) == obj->head;
}bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {if(myCircularQueueIsFull(obj)){return false;}obj->a[obj->tail] = value;obj->tail++;obj->tail %= (obj->k+1);return true;
}bool myCircularQueueDeQueue(MyCircularQueue* obj) {if(myCircularQueueIsEmpty(obj)){return false;}obj->head++;obj->head %= (obj->k+1);return true;
}int myCircularQueueFront(MyCircularQueue* obj) {if(myCircularQueueIsEmpty(obj)){return -1;}elsereturn obj->a[obj->head];
}int myCircularQueueRear(MyCircularQueue* obj) {if(myCircularQueueIsEmpty(obj)){return -1;}elsereturn obj->a[(obj->tail -1 + obj->k +1) % (obj->k+1)];//return obj->tail == 0 ? obj->a[obj->k]:obj->a[obj->tail-1];
}void myCircularQueueFree(MyCircularQueue* obj) {free(obj->a);free(obj);
}/*** Your MyCircularQueue struct will be instantiated and called as such:* MyCircularQueue* obj = myCircularQueueCreate(k);* bool param_1 = myCircularQueueEnQueue(obj, value);* bool param_2 = myCircularQueueDeQueue(obj);* int param_3 = myCircularQueueFront(obj);* int param_4 = myCircularQueueRear(obj);* bool param_5 = myCircularQueueIsEmpty(obj);* bool param_6 = myCircularQueueIsFull(obj);* myCircularQueueFree(obj);
*/