【leetcode】6. Z 字形变换

 

char * convert(char * s, int numRows){
    if (numRows < 2)
        return s;
    int i, j, pst = 0, len = strlen(s), cnt;
    char* retStr = (char*)calloc(len + 1, sizeof(char));
    for (i = 0; i<numRows; i++){
        for (j = i, cnt = 0; j<len; cnt++){
            retStr[pst++] = s[j];
            j += ( i!=numRows-1 && (cnt % 2 == 0 || i == 0) ) ? (numRows - 1 - i) * 2 : i * 2;
        }
    }
    /*for (i = numRows - 1; i<len; i += (numRows - 1) * 2)
        retStr[pst++] = s[i];*/
    return retStr;
}

 

posted @ 2020-12-07 00:20  温暖了寂寞  阅读(74)  评论(0编辑  收藏  举报