/*
Input: s = "PAYPALISHIRING",
numRows = 4
Output: "PINALSIGYAHRPI"
注意:一定判断numrows 长度 是1的话 直接return;
建立一个指向性的Boolean值名字为upAndDown 判断 +1 或者 -1;
for循环 每次到第一行或者最后一行 就翻转upAndDown的值;
*/
class Solution {
public String convert(String s, int numRows) {
if(numRows == 1) return s;
ArrayList rows = new ArrayList<> ();
int n = Math.min(s.length(), numRows);
for(int i = 0; i < n; i++){
rows.add(new StringBuilder());
}
Boolean upAndDown = false;
int curRow = 0;
for(int i = 0; i < s.length(); i++){
rows.get(curRow).append(s.charAt(i));
if(curRow == 0 || curRow == numRows - 1) {
upAndDown = !upAndDown;
}
curRow += upAndDown ? (+1) : (-1);
}
StringBuilder result = new StringBuilder();
for(StringBuilder b : rows){
result.append(b);
}
return result.toString();
}
}