// The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

// P   A   H   N
// A P L S I I G
// Y   I   R
// And then read line by line: "PAHNAPLSIIGYIR"
// Write the code that will take a string and make this conversion given a number of rows:

// string convert(string text, int nRows);
// convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

public class Solution {
    public String convert(String s, int nRows) {
    	int len = s.length();

    	if(len <= 1 || nRows == 1){
    		return s;
    	}

    	char[] chars = new char[len];
    	int counter = 0;

    	for(int i = 0; i < nRows; i++){
    		for(int cycle = i; cycle < len; cycle += (nRows - 2) * 2 + 2){
    			chars[counter++] = s.charAt(cycle);

    			if(i > 0 && i < nRows - 1){
    				int fakeRow = nRows - i;
    				int mid = cycle + (fakeRow - 2) * 2 + 2;

    				if(mid < len){
    					chars[counter++] = s.charAt(mid);
    				}
    			}
    		}
    	}

    	return new String(chars);
	}
}
@@ -1085,8 +1085,1009 @@
 s);%0A%09%7D%0A%7D
+%0A%0Apublic class Solution %7B%0A    public String convert(String s, int nRows) %7B%0A        int len = s.length();%0A        if (len == 0 %7C%7C nRows == 1) %7B%0A            return s;%0A        %7D%0A        %0A        List%3CStringBuilder%3E res = new ArrayList%3CStringBuilder%3E();%0A        for (int i = 0; i %3C nRows; i++) %7B%0A            res.add(new StringBuilder());%0A        %7D%0A        %0A        int cycle = (nRows - 1) * 2;%0A        %0A        for (int i = 0; i %3C len; i++) %7B%0A            int pos = i %25 cycle;%0A            if (pos %3C nRows) %7B%0A                res.get(pos).append(s.charAt(i));%0A            %7D else %7B%0A                // offSet is pos's mirror index regarding to the bottom.%0A                int offSet = 2 * (nRows - 1) - pos;%0A                res.get(offSet).append(s.charAt(i));%0A            %7D%0A        %7D%0A        %0A        StringBuilder sb = new StringBuilder();%0A        for (StringBuilder temp : res) %7B%0A            sb.append(temp.toString());%0A        %7D%0A        %0A        return sb.toString();%0A    %7D%0A%7D%0A            %0A        %0A        

update zigzag conversion