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 s, int numRows);
解题思路:暴力解决....把字符串按照要求排一遍
class Solution {
public String convert(String s, int numRows) {
String str="";
int len=s.length();
if(numRows==1)
return s;
else
{
int i=0;
int x=0;
int y=0;
int flag=0;
char ch[][]=new char[len][numRows];
while(i<len)
{
while(y<numRows&&flag==0&&i<len)
{
ch[x][y]=s.charAt(i);
i++;
y++;
}
flag=1;
y--;
while(y>0&&flag==1&&i<len)
{
x++;
y--;
ch[x][y]=s.charAt(i);
i++;
}
y++;
flag=0;
}
for(int j=0;j<numRows;j++)
{
for(int k=0;k<len;k++)
{
if(ch[k][j]>0)
str=str+String.valueOf(ch[k][j]);
}
}
return str;
}
}
}