ZigZag Conversion
July 27, 2020
ZigZag Conversion
class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
char[] result = new char[s.length()];
int term = (numRows - 1) * 2;
int index = 0;
for (int i = 0; i < numRows; i++) {
int tempTerm = i * 2;
for (int j = i; j < s.length(); j = j + tempTerm) {
result[index] = s.charAt(j);
index++;
tempTerm = term - tempTerm == 0 ? term : term - tempTerm;
}
}
return String.valueOf(result);
}
}