**idea**

if we could know the index of each character which is in the original string,the problem will be solved easily.Now we need to find the regulationin the conversion process by some example.

**example**

e.g1:

string：“abcdefabcdef” nRows:4

after conversion:

as shown in the figure above,the old string was divided into two units and every unit was integrated which is we want to see.

obviously,the result String is "aabfbfcecedd",but it is most primary purposes to find the law.

It is not difficult to find that when we know the "nRows",we can figure out a lot of information include "nCols" which refer to the unit count,int this example,the "nCols" is 2,the quantity of total characters in a unit,the relationship between index ,"nRows" and "nCols".

Law summary：

Assuming the "nRows" is n,the quantity of total characters in a unit is n+n-2,being 2n-2.the "nCols" is {s.length()/(2n-2)} ,{x} expressed the smallest integer that is not less than x.

the index law:

when we konw the index of the first unit,the rest of unit will be determinated.

now we focus that how to express first unit by "n" when "nRows" is "n".

the first unit index:

now we have konwn all index of characters in the new String "pciture" after Zigzag conversion.

then what we need to do is to write into "result string" line by line according the "picture".it is easy.

All of these are based on conditions that the "s" can be divided into some unbroken units.however if the "s" doesn't satisfy this condition,what should we do?

e.g2:

string：“abcdefabcdefg” nRows:4

after conversion,we konw the last unit only have one character “g”.some errors will occurre in the program under the circumstances.

Don't worry,we can sovle by adding right quantity of character such as "#" behind the original s

in this e.g. ,we need to add 5 "#",then s= “abcdefabcdefg#####”.After that,Follow the steps of example 1,finally we will get "reslut string" include the characters we added.It is true "result String" after getting rid of these redundant characters.

**Java Code**

public String convert(String s, int numRows) {

if(s==""||numRows==1){

return s;

}

String rString="";

int l=s.length();

int numCols=(int) Math.ceil((double)l/(double)(2*numRows-2));
int jiange=2*numRows-2;

while(numCols*(2

*numRows-2)>l){*

s=s+"#";

l++;

}

//first row

for(int col=0;col<numCols;col++){

rString=rString+s.charAt(0+coljiange);

s=s+"#";

l++;

}

//first row

for(int col=0;col<numCols;col++){

rString=rString+s.charAt(0+col

}

//form seconde to numRows-1 row

for(int row=2;row<=numRows-1;row++){

for(int col=0;col<numCols;col++){

rString=rString+s.charAt(row-1+col

*jiange)+s.charAt(2*numRows-row-1+col

*jiange);*

}

}

//the nRows row

for(int col=0;col<numCols;col++){

rString=rString+s.charAt(numRows-1+coljiange);

}

}

//the nRows row

for(int col=0;col<numCols;col++){

rString=rString+s.charAt(numRows-1+col

}

System.out.println("one to nRows row:"+rString);

return rString.replace("#", "");

`}`