leetcode 6. ZigZag Conversion
It's too tired preparing for interview while working. So I had a totally relaxing, leetcode-free weekend.
As for leetcode 6, the most stable way to solve this problem is attached below. And I didn't try to figure out the formula with a lot of % like when I was at my 20's.
class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) return s;
StringBuilder[] ret = new StringBuilder[numRows];
for (int i = 0; i < numRows; ++i) ret[i] = new StringBuilder();
int row = 0;
int dir = 1;
for (int i = 0; i < s.length(); ++i) {
ret[row].append(s.charAt(i));
row += dir;
if (row == numRows - 1) dir = -1;
else if (row == 0) dir = 1;
}
String result = "";
for (StringBuilder sb: ret) {
result += sb.toString();
}
return result;
}
}