ZigZag Conversion
Updated:
Contents
给出形为下图的字符的横排顺序。
0 * * * 8 * * * 16
1 * * 7 9 * * 15 17
2 * 6 * 10 * 14 * 18
3 5 * * 11 13
4 * * * 12
循环为2n-2,其中n为列数。
找规律。
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”。
public class Solution {
public String convert(String s, int numRows) {
if(numRows <= 1){
return s;
}
String[] res= new String[numRows];
Arrays.fill(res, "");
int j, i=0 ,times = 2 * numRows - 2;
while(i < s.length()){
for(j = 0; j < numRows && i < s.length(); j++){
res[j] += s.charAt(i);
i++;
}
for(j = numRows - 2; j > 0 && i < s.length(); j--){
res[j] += s.charAt(i);
i++;
}
}
String va = "";
for (int k = 0; k < numRows; k++){
va += res[k];
}
return va;
}
}