//时间复杂度为O(n),运行时间为351ms
public 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;
}
}
ZigZag Conversion
最新推荐文章于 2020-03-02 00:02:09 发布