这道题是要找出每个字符跳转的规律,还是比较好找的 关键是找到zigGap和zagGap
public class Solution {
public String convert(String s, int numRows) {
if (s.equals("")) {
return s;
}
if (numRows == 0) {
return "";
}
if (numRows == 1) {
return s;
}
int maxGap = 2 * numRows - 2;
char[] resultChars = new char[s.length()];
int zigGap = maxGap;
int zagGap = 0;
int start = 0;
int q = 0;
while (zigGap >= 0) {
int p = start;
if (p >= s.length())
break;
resultChars[q] = s.charAt(p);
q++;
while (p < s.length()) {
p = p + zigGap;
if (p >= s.length())
break;
if (zigGap > 0) {
resultChars[q] = s.charAt(p);
q++;
}
p = p + zagGap;
if (p >= s.length())
break;
if (zagGap > 0) {
resultChars[q] = s.charAt(p);
q++;
}
}
zigGap = zigGap - 2;
zagGap = zagGap + 2;
start++;
}
String resultString = new String(resultChars);
return resultString;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。