ZigZag Conversion

来源:互联网 发布:h265网络直播编码器 编辑:程序博客网 时间:2024/05/16 04:03

//时间复杂度为O(n),运行时间为351mspublic class Solution {public String convert(String s, int nRows) {        char str[] = s.toCharArray();        int len = str.length;        if ((len == 0) || (nRows == 1) || (len <= nRows)){        return s;        }        int i = 0;        int j = 0;        int p;        int q;        int r = nRows - 2 + nRows;        char[] newstr = new char[len];         while (i < len && (j < len)){        q = j;        newstr[i ++] = str[j];        j += r;        p = 1;        while (i < len) {            if (q > 0 && (q < nRows - 1) && (r * p - q < len)){            newstr[i ++] = str[r * p - q];            }            if ((i < len) && (j < len)){            newstr[i] = str[j];            j += r;            i ++;            p ++;            }            else {            break;               }        }           j = q;            j ++;        }        String s1 = new String(newstr);    return s1;    }}



0 0