llllmz

导航

76. 最小覆盖子串c

bool judge(int* temps,int* tempt){
    for(int i=0;i<200;i++){
        if(temps[i]<tempt[i]) return false;
    }
    return true;
}

char* minWindow(char* s, char* t) {
    int ns=strlen(s),nt=strlen(t);\
    char* array1=(char*)malloc(sizeof(char));
    array1[0]=0;
    if(ns<nt) return array1;
    int minhead=0,head=0,minend=0,end=0,min=INT_MAX,sum=0;
    int temps[200]={0};
    int tempt[200]={0};
    for(int i=0;i<nt;i++){
        tempt[t[i]-'A']++;        
    }
    while(head<=end&&end<=ns){
        printf("%d %d %d   ",minhead,minend,min);
        if(judge(temps,tempt)){
            if(sum<min){
                min=sum;
                minhead=head;
                minend=end;
            }
            temps[s[head++]-'A']--;
            sum--;
        }else{
            if(end==ns) break;
            temps[s[end++]-'A']++;
            sum++;
        }        
    }
    if(min==INT_MAX) return array1;
    char* array=(char*)malloc(sizeof(char)*(min+1));
    for(int i=0;i<min+1;i++) array[i]=0;
    for(int i=0;i<min;i++){
        array[i]=s[i+minhead];
    }    
    return array;
}

字符串MALLOC的坑!!!一定要n+1个且一定要初始话!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

posted on 2024-03-13 15:24  神奇的萝卜丝  阅读(36)  评论(0)    收藏  举报