package LeetCode;

import java.util.ArrayList;

public class ZigZagConversion {
	public String convert(String s, int nRows) {
		ArrayList<String[]> charContainer = new ArrayList<String[]>();

		String[] tempCharArray = new String[nRows];
		int tempCharIndex = 0;
		int constIndex = Math.max((int) (nRows / 2 - 1), 0);
		
		for (int i = 0; i < s.length(); i++) {
			if (tempCharIndex < nRows) {
				tempCharArray[tempCharIndex] = s.substring(i, i + 1);
				tempCharIndex++;
			} else {
				charContainer.add(tempCharArray);
				tempCharArray = new String[nRows];
				tempCharArray[constIndex] = s.substring(i, i + 1);
				charContainer.add(tempCharArray);
				tempCharArray = new String[nRows];
				tempCharIndex = 0;
			}
		}
		charContainer.add(tempCharArray);

		StringBuilder sb = new StringBuilder();
		int r = 0, c = 0;
		while (r < nRows) {
			while (c < charContainer.size()) {
				if(charContainer.get(c)[r] != null){
					sb.append(charContainer.get(c)[r]);
				}
				c++;
			}
			c = 0;
			r++;
		}
		
		return sb.toString();
	}
}

@@ -237,88 +237,58 @@
 ;%0A%09%09
-int tempCharIndex = 0;%0A%09%09int constIndex = Math.max((int) (nRows / 2 - 1),
+boolean toDown = true;%0A%09%09int currentPosition =
  0
-)
 ;%0A
-%09%09
 %0A%09%09f
@@ -337,28 +337,13 @@
 f (t
-empCharIndex %3C nRows
+oDown
 ) %7B%0A
@@ -360,29 +360,31 @@
 arArray%5B
-tempCharIndex
+currentPosition
 %5D = s.su
@@ -410,37 +410,121 @@
 %09%09%09%09
-tempCharIndex++;%0A%09%09%09%7D else %7B%0A
+currentPosition++;%0A%09%09%09%09if (currentPosition == nRows) %7B%0A%09%09%09%09%09currentPosition = Math.max(0, currentPosition - 2);%0A%09
 %09%09%09%09
@@ -549,32 +549,33 @@
 tempCharArray);%0A
+%09
 %09%09%09%09tempCharArra
@@ -589,32 +589,113 @@
  String%5BnRows%5D;%0A
+%09%09%09%09%09if (currentPosition != 0) %7B%0A%09%09%09%09%09%09toDown = false;%0A%09%09%09%09%09%7D%0A%0A%09%09%09%09%7D%0A%09%09%09%7D else %7B%0A
 %09%09%09%09tempCharArra
@@ -701,17 +701,22 @@
 ay%5Bc
-onstIndex
+urrentPosition
 %5D = 
@@ -734,24 +734,80 @@
 (i, i + 1);%0A
+%09%09%09%09currentPosition = Math.max(0, currentPosition - 1);%0A
 %09%09%09%09charCont
@@ -879,26 +879,61 @@
 %09%09%09%09
-tempCharIndex
+if (currentPosition
  =
+=
  0
-;
+) %7B%0A%09%09%09%09%09toDown = true;%0A%09%09%09%09%7D
 %0A%09%09%09
@@ -1023,44 +1023,33 @@
 ;%0A%09%09
-int r = 0, c = 0;%0A%09%09while (r
+for (int j = 0; j
  %3C nRows
 ) %7B%0A
@@ -1048,23 +1048,37 @@
 Rows
+; j++
 ) %7B%0A%09%09%09
-while (c
+for (int i = 0; i
  %3C c
@@ -1096,16 +1096,21 @@
 r.size()
+; i++
 ) %7B%0A%09%09%09%09
@@ -1111,16 +1111,17 @@
 %7B%0A%09%09%09%09if
+ 
 (charCon
@@ -1131,20 +1131,20 @@
 ner.get(
-c)%5Br
+i)%5Bj
 %5D != nul
@@ -1145,16 +1145,17 @@
 != null)
+ 
 %7B%0A%09%09%09%09%09s
@@ -1185,12 +1185,12 @@
 get(
-c)%5Br
+i)%5Bj
 %5D);%0A
@@ -1202,43 +1202,13 @@
 %0A%09%09%09
-%09c++;%0A%09%09%09%7D%0A%09%09%09c = 0;%0A%09%09%09r++;%0A%09%09
 %7D%0A%09%09
+%7D
 %0A%09%09r
@@ -1226,15 +1226,22 @@
 String()
+.trim()
 ;%0A%09%7D%0A%7D%0A

modify zigzag