I handwritten the brute force algorithm and then copied it to the computer. I haven’t done the input and output test, but the idea should be correct. You can just look at the idea. The 408 exam does not require ac. If you have any questions, please correct me and learn together!
The order of the exercises refers to the chapter order of the 22nd edition of the Book of Kings
year 2010
void Move(int R[],int p,int n){
//lenth=n
int L[n]={
0}; //Create auxiliary array
for(int i=0;i<n;i++){
if(i-p>=0) L[i-p]=R[i]; //Assign a value to the corresponding position of the auxiliary array
else L[n+i-p]=R[i];
}
for(int i=0;i<n;i++) R[i]=L[i];
}
Time complexity O(n), space complexity O(n)
year 2011
int midSerch(int a[],int b[],int n){
int c[2*n]={
0};
int i;
for(i=0;i<2*n;i++){
if(i<n) c[i]=a[i];
else c[i]=b[i-n];
quickSort(c,0,2*n-1);
return c[n-1];
}
}
void quickSort(int q[],int l,int r){
//Quick sort template
if(l>=r) return;
int x=q[l + r >> 1],i=l,j=r;
while(i<j){
do i ++ ; while (q[i] < x);
do j -- ; while (q[j] > x);
if(i<j) swap(q[i],q[j]);
}
quick_sort(q,l,i-1);
quick_sort(q,i,r);
}
year 2013
int mainElement(int A[],int n){
int B[n]={
0},i;
for(i=0;i<n;i++) B[A[i]]++;
int max=0;
for(i=0;i<n;i++){
//Traverse the auxiliary array to find the maximum number of occurrences
if(B[i]>B[max]) max=i;
}
if(B[max>n/2]) return max;
else return -1;
}
Time complexity O(n), space complexity O(n)
2018
int findMin(int L[],int n){
int i=1,k=0;
while(i<=n){
for(k=0;k<n;k++){
if(L[k]==i){
i++;
break;
} //找到后,+1跳出循环
}
if(k==n) return i; //若k=n-1,说明没找到,直接返回
}
return i;
}
Time complexity O(n^2), space complexity O(1)
2020
int solution(int a[],int b[],int c[],int n1,int n2,int n3){
int i=0,j=0,k=0,dis=0;
int min=99999;
for(i=0;i<n1;i++){
for(int j=0;j<n2;j++){
for(int k=0;k<n3;k++){
dis=abs(a[i]-b[j])+abs(b[j]-c[k])+abs(c[k]-a[i]);//abs为求绝对值函数
if(dis<min) min=dis;
}
}
}
return dis;
}
int abs(int a){
if(a<0) return -a;
else return a;
}
Time complexity O(n^3) space O1
Year 2009
/*
设置两个指针p,q,一个变量count,p一直遍历到最后,count负责计数,
然后将q遍历到count-k次,最后输出
*/
typedef struct LNode{
int data;
struct LNode *link;
}LNode,*Linklist
int SearchLink(Linklist list,int k){
int count=0;
Lnode *p=list->link,*q=*p;
while(p->link!=NULL){
count++;
p=p->link;
}
if(count<k) return 0;
while(count-k>0){
q=q->link;
count--;
}
printf("%d",q->data);
return 1;
}
Time complexity O(mn), space complexity O(1)
2012
Idea: Set up two arrays p, q, respectively pointing to the two head nodes of the linked list, two loops, p one step backward, q traverses until they are equal
typedef struct LNode{
char data,
struct LNode *next,
}*LinkList,LNode;
LNode *same_tail(LinkList str1,LinkList str2){
LNode *p=str1->next,*q=str2->next;
while(p->next!=NULL){
while(q->next!=NULL){
if(p==q) return p;
q=q->next;
}
q=str2->next;
p=p->next;
}
}
Time complexity O(mn)
If someone watches it, it will be updated slowly later, if no one watches it, forget it