衢州站電話重慶疫情最新情況
文章目錄
- 鏈表分割
- 環(huán)形鏈表
- 有效的括號
鏈表分割
鏈接: 鏈表分割
雖然這個(gè)題??途W(wǎng)中只有C++,但是無所謂,我們只要知道C++是兼容C的就可以了
至于說這個(gè)題的思路,我們就弄兩個(gè)鏈表,把小于x的結(jié)點(diǎn)放到一個(gè)鏈表中,剩下的放到另一個(gè)鏈表中,最后把兩個(gè)鏈表串起來就可以了
實(shí)現(xiàn)方式的話有兩種,一種是鏈表帶哨兵位,一種是不帶,帶哨兵位的話處理起來比較簡單,很多判斷條件是不需要的,不帶的話就相對要麻煩一些,但是你如果對鏈表的操作比較熟悉的話,其實(shí)還行
下面是帶哨兵位的實(shí)現(xiàn)
class Partition {
public:ListNode* partition(ListNode* pHead, int x) {struct ListNode *cur=pHead;struct ListNode *head1,*head2,*tail1,*tail2;//1的是小值,2的是大值//開辟哨兵位head1=tail1=(struct ListNode *)malloc(sizeof(struct ListNode ));head2=tail2=(struct ListNode *)malloc(sizeof(struct ListNode ));
while(cur){struct ListNode *next=cur->next;if(cur->val<x){tail1->next=cur;tail1=tail1->next;tail1->next=NULL;}else{tail2->next=cur;tail2=tail2->next;tail2->next=NULL;}cur=next;
}
//把兩個(gè)鏈表接到一起,如果第一個(gè)鏈表為空也無所謂
tail1->next=head2->next;struct ListNode *head=head1->next;free(head1);//沒用的就free掉free(head2);
return head;}
};
下面是不帶哨兵位的實(shí)現(xiàn)
class Partition {
public:ListNode* partition(ListNode* pHead, int x) {struct ListNode *cur=pHead;struct ListNode *head1=NULL,*head2=NULL,*tail1=NULL,*tail2=NULL;//一般都是定義兩組指針,一組管理一個(gè)鏈表while(cur){struct ListNode *tmp=cur;cur=cur->next;if(tmp->val<x){
if(tail1==NULL){//如果鏈表為空head1=tail1=tmp;tail1->next=NULL;
}
else{tail1->next=tmp;tail1=tail1->next;tail1->next=NULL;
}}else{
if(tail2==NULL){head2=tail2=tmp;tail2->next=NULL;
}
else{tail2->next=tmp;tail2=tail2->next;tail2->next=NULL;
}}}//合并兩個(gè)鏈表if(head1==NULL){//判斷第一個(gè)表是否為空return head2;}else{tail1->next=head2;return head1;}}
};
環(huán)形鏈表
鏈接:環(huán)形鏈表
這個(gè)題的話需要一些數(shù)學(xué)推理去找它們之間的關(guān)系,要不然不太好說
我們假如說起始位置到入環(huán)處的距離為a,入環(huán)出到相遇處距離為b,環(huán)的周長為c,在之前,我們已經(jīng)能判斷一個(gè)鏈表中是否有環(huán)了,就是通過兩個(gè)指針,一個(gè)fast一回走兩步,一個(gè)slow一回走一步,那么它們兩個(gè)之間有一定的數(shù)學(xué)關(guān)系,就是2*(a+b)=a+nc+b,化簡一下為c-b+c(n-1)=a,這是什么意思啊?就是一個(gè)指針從頭走,一個(gè)指針從相遇處走,以相同的速度,最后就會在那個(gè)相遇入環(huán)點(diǎn)相遇
下面我們來實(shí)現(xiàn)一下
struct ListNode *detectCycle(struct ListNode *head) {struct ListNode *fast=head,*slow=head;
while(fast&&fast->next){//能出循環(huán)就沒有環(huán),有環(huán)在循環(huán)中返回
fast=fast->next->next;
slow=slow->next;if(fast==slow){
struct ListNode *tmp=fast;
while(tmp!=head){//相同時(shí)停止,并返回這個(gè)點(diǎn)
tmp=tmp->next;
head=head->next;
}
return head;}
}
return NULL;
}
我們之前找過兩個(gè)相交鏈表的相交位置,這里我們?nèi)绻严嘤鳇c(diǎn)的前一個(gè)位置的next置為空,就可以了,需要注意前一個(gè)只能是fast的前一個(gè)
下面我們來實(shí)現(xiàn)一下
//找相交點(diǎn)的函數(shù)
//思路就是計(jì)算兩個(gè)鏈表的長度,長的先走差的長度數(shù),最后同時(shí)走,相同了就找到了
struct ListNode *meetpoint(struct ListNode *s1,struct ListNode *s2){int num1=0;int num2=0;struct ListNode *head1=s1,*head2=s2;while(head1){num1++;head1=head1->next;}while(head2){num2++;head2=head2->next;}struct ListNode *thelong=s1,*theshort=s2;if(num2>num1){thelong=s2;theshort=s1;}int num=abs(num1-num2);//求差的長度數(shù)while(num){thelong=thelong->next;num--;}while(thelong!=theshort){thelong=thelong->next;theshort=theshort->next;}return thelong;
}struct ListNode *detectCycle(struct ListNode *head) {struct ListNode *fast=head,*slow=head;
while(fast&&fast->next){struct ListNode *fastprev=fast->next;//記錄一下fast的上個(gè)位置
fast=fast->next->next;slow=slow->next;if(fast==slow){fastprev->next=NULL;return meetpoint(head,slow);}
}
return NULL;
}
有效的括號
鏈接:有效的括號
這個(gè)題是用棧來實(shí)現(xiàn)的,正好利用了棧的特性,左括號入棧,右括號判斷出棧,如果不匹配就返回false
bool isValid(char * s){ST st;STInit(&st);
while(*s){if(*s=='('||*s=='['||*s=='{'){//左括號入棧STPush(&st,*s);}else{if(STEmpty(&st)){//棧為空并且要處理的字符為右括號STDestroy(&st);return false;}char tmp=STTop(&st);//匹配棧頂元素和要處理的元素if(*s==')'&&tmp!='('||*s==']'&&tmp!='['||*s=='}'&&tmp!='{'){return false;}else{STPop(&st);}}s++;
}
if(!STEmpty(&st)){//true的話最后棧應(yīng)該為空
STDestroy(&st);return false;
}
else{STDestroy(&st);return true;
}
}
我們在這個(gè)函數(shù)之前是需要自己創(chuàng)建棧的,并且要實(shí)現(xiàn)它的一些功能,我們之前也有代碼,可以看之前的博客
鏈接:棧和隊(duì)列