https://leetcode.com/problems/zigzag-conversion/?tab=Description
单词按照N行 Z型排列之后,按行遍历返回结果
找规律
public class Solution {
public String convert(String s, int numRows) {
if (s == null || s.length() <= numRows || numRows == 1) {
return s;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numRows; i++) {
int j = i;
int count = 0;
while (j < s.length()) {
sb.append(s.charAt(j));
if (i == 0 || i == numRows - 1) {
j += 2 * numRows - 2;
} else {
j += 2 * numRows - 2 - 2 * (count % 2 == 0 ? i : numRows - i - 1);
}
count++;
}
}
return sb.toString();
}
}
转载请注明原文地址: https://ju.6miu.com/read-13302.html