# Accepted Java Solution W/ Explanations

• ``````public class Solution
{
/*
* From at least 2 rows, the first characters in first row & last row are going to skip numRows - 1 + numRows - 2 characters (maxSkip).
* From the 2nd row to the penultimate row (if there is one), the skip range (maxSkipLimit) is 1 less than maxSkip.
* The skip is going to alter between current maxSkip (skip1),
* and maxSkipLimit - current maxSkip (skip2) while the maxSkip decrease by 2 after each row.
*
* a   g    m
* b  fh   ln
* c e i k  o
* d   j    p
*
* 4 rows, so the maxSkip is gonna be 4 * 2 - 3 = 5.  So the 'a' in first row is gonna skip 5 letters and add the one after 5 letters,
* which is 'g' in this case.  And going on for 'm'.  Same for 'd' in the last row.
*
* For 2nd row, the maxSkipLimit is gonna be 5 - 1 = 4;  The maxSkip is 5 - 2 = 3;  So the skip is gonna alter between 3 and 4 - 3.
*
* For 3nd row, the maxSkipLimit is still 5 - 1 = 4;  The maxSkip is 3 - 2 = 1;  So the skip is gonna alter between 1 and 4 - 1.
*/
public String convert(String s, int numRows)
{
if(numRows <= 1 || s.length() <= numRows)
return s;

StringBuilder sb = new StringBuilder();
String ans = "";
int maxSkip = numRows - 1 + numRows - 2;
for(int i = 0; i < s.length(); i += (maxSkip + 1))
sb.append(s.charAt(i));

ans += sb.toString();
sb.setLength(0);
int maxSkipLimit = maxSkip - 1;
maxSkip -= 2;
for(int i = 1; i < numRows - 1; i++)
{
int skip1 = maxSkip;
int skip2 = maxSkipLimit - skip1;
int curSkip = skip1;
int j = i;
sb.append(s.charAt(i));
while(j < s.length())
{
if(curSkip == skip1)
{
j += (curSkip + 1);
if(j < s.length())
{
sb.append(s.charAt(j));
curSkip = skip2;
}
}
if(curSkip == skip2)
{
j += (curSkip + 1);
if(j < s.length())
{
sb.append(s.charAt(j));
curSkip = skip1;
}
}
}
ans += sb.toString();
sb.setLength(0);
if(maxSkip != 1)
maxSkip -= 2;
}

maxSkip = maxSkipLimit + 1;
for(int i = numRows - 1; i < s.length(); i += (maxSkip + 1))
sb.append(s.charAt(i));
ans += sb.toString();
return ans;
}
}``````

• {public String convert(String s, int numRows){
if (s.length() <= numRows || numRows <= 1)
return s;

``````String result = "";
StringBuilder sb = new StringBuilder();
int maxskip = 2*numRows - 2;
for(int i = 0; i < s.length(); i += maxskip) {
sb.append(s.charAt(i));
}
result += sb.toString();

sb.setLength(0);
int skip1 = 2*numRows - 4;
int skip2 = 2;
int curskip = skip1;
for(int i = 1; i < numRows -1; i++){
sb.append(s.charAt(i));
for (int j = i; j < s.length(); ){
if (curskip == skip1){
j += curskip;
if (j < s.length()) {
sb.append(s.charAt(j));
curskip = skip2;
}
}
if (curskip == skip2){
j += curskip;
if (j < s.length()) {
sb.append(s.charAt(j));
curskip = skip1;
}
}
}
result += sb.toString();
sb.setLength(0);
if(skip1 >= 2) {
skip1 -= 2;
skip2 += 2;
}
}

sb.setLength(0);
for (int i = numRows -1; i < s.length(); i += maxskip){
sb.append(s.charAt(i));
}
result += sb.toString();

return result;
``````

}}

• This post is deleted!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.