题意:
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 RAnd then read line by line:
"PAHNAPLSIIGYIR"
思路:根据题意模拟,找下规律即可;
class Solution
{
public:
string convert(string s, int numRows) {
if(numRows==1)return s;
int n=s.size();
int z=(numRows-1)*2,y=0;
string ans="";
for(int i=1;i<=numRows;i++)
{
if(z==0||y==0)
for(int j=i;j<=n;j+=max(y,z))
ans+=s[j-1];
else
{
int flag=0;
for(int j=i;j<=n;)
{
ans+=s[j-1];
if(!flag)j+=z,flag=1;
else j+=y,flag=0;
}
}
z-=2;
y+=2;
}
return ans;
}
};