从零开始的leetcode刷题——第6题

char* convert(char* s, int numRows) {
    int len=strlen(s);
    int *num;
    int i=0,j=1,k=0;
    char *p;
    int temp=0;
    
    if(numRows==1) return s;
    
    num=(int*)malloc(len*sizeof(int));
    p=(char*)malloc( (len+1)*sizeof(char));

    while( i<len ){

        if( !temp ){
            num[i++] = j++;
        }else {
            num[i++] = j--;
        }

        if( j > numRows ) {
            j-=2;
            temp = 1;
        }
        if( j < 1 ) {
            temp = 0;
            j+=2;
        }
    }

    for(i=1;i<=numRows;i++){

        for(j=0;j<len;j++){

           if(num[j]==i){
               p[k++]=s[j];
           }
        }
    }
    p[k]='\0';
    return p;
}

 

从零开始的leetcode刷题——第6题