# Easy-题目61：6. ZigZag Conversion

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”.

nRows=3时：

nRows=4时：

public class Solution {
public String convert(String s, int numRows)
{
if(numRows==1)
return s;
else    {
int step1=(numRows-1)*2;
int step2=0;

for(int i=0;i<numRows;i++)  {
String line="";
int j=i;
boolean isStep1=true;
while(j<s.length()) {
if(step2==0)    {
line+=s.charAt(j);
j+=step1;
}
else if(step1==0) {
line+=s.charAt(j);
j+=step2;
}
else    {
line+=s.charAt(j);
if(isStep1)
j+=step1;
else
j+=step2;
isStep1=!isStep1;
}
}
step1-=2;
step2+=2;
}
}
}
}

33ms,beats 25.35%,众数8ms,15.13%
cmershen的碎碎念：

• 本文已收录于以下专栏：

举报原因： 您举报文章：Easy-题目61：6. ZigZag Conversion 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)