寻找W型字符的规律:除第一和最后一行外都是对称的。找出循环的长度相应的做出位移即可。比较烦
1 public class Solution { 2 public String convert(String s, int nRows) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(nRows == 1) 6 return s; 7 char[] mychar = s.toCharArray(); 8 char[] result = new char[mychar.length]; 9 int rows = 2 * nRows - 2; 10 int index = 0; 11 for(int i = 1; i < nRows + 1; i++) 12 { 13 int start = i - 1; 14 boolean odd = true; 15 while(start < result.length){ 16 result[index] = mychar[start]; 17 if(i == 1 || i == nRows) 18 start += rows; 19 else if(odd) 20 { 21 start += rows - 2 * i + 2; 22 odd = false; 23 } 24 else 25 { 26 start += 2 * i - 2; 27 odd = true; 28 } 29 index++; 30 } 31 } 32 return String.valueOf(result); 33 34 } 35 }