您的位置:首页 > 其它

6. ZigZag Conversion

2016-05-26 12:08 232 查看
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"
.

/*n=numRows
Δ=2n-2    1                           2n-1                         4n-3
Δ=        2                     2n-2  2n                    4n-4   4n-2
Δ=        3               2n-3        2n+1              4n-5       .
Δ=        .           .               .               .            .
Δ=        .       n+2                 .           3n               .
Δ=        n-1 n+1                     3n-3    3n-1                 5n-5
Δ=2n-2    n                           3n-2                         5n-4
*/


that's the zigzag pattern the question asked! Be careful with nR=1 && nR=2
class Solution {
public:
string convert(string s, int numRows) {
string result="";
if(numRows==1)
return s;
int step1,step2;
int len=s.size();
for(int i=0;i<numRows;++i){
step1=(numRows-i-1)*2;
step2=(i)*2;
int pos=i;
if(pos<len)
result+=s.at(pos);
while(1){
pos+=step1;
if(pos>=len)
break;
if(step1)
result+=s.at(pos);
pos+=step2;
if(pos>=len)
break;
if(step2)
result+=s.at(pos);
}
}
return result;
}
};

use numRows space and easy to understand ,while my answer search the math solution,not easy to understand ,but is
interesting


string convert(string s, int numRows)
{
int length = s.size();
if(numRows <= 1 || length <= numRows)
return s;
string sZigzag;
for(int iLineIndex = 0; iLineIndex < numRows; iLineIndex++)
{
int iRowIndex = iLineIndex;
for(; iRowIndex < length; iRowIndex += 2 * (numRows - 1))
{
if(0 == iLineIndex || numRows - 1 == iLineIndex)
sZigzag = sZigzag + s[iRowIndex];
else
{
if(iRowIndex == iLineIndex)
sZigzag = sZigzag + s[iRowIndex];
else
{
sZigzag = sZigzag + s[iRowIndex - 2 * iLineIndex];
sZigzag = sZigzag + s[iRowIndex];
}
}
}
if((iLineIndex > 0) && (iLineIndex < numRows - 1) && (iRowIndex - 2 * iLineIndex < length))
sZigzag = sZigzag + s[iRowIndex - 2 * iLineIndex];
}
return sZigzag;
}

The problem statement itself is unclear for many. Especially for 2-row case. "ABCD", 2 --> "ACBD". The confusion most likely is from the character placement. I would like to extend it a little bit to make ZigZag easy understood.

The example can be written as follow:
P.......A........H.......N
..A..P....L..S....I...I....G
....Y.........I........R

Therefore, <ABCD, 2> can be arranged as:
A....C
...B....D

My simple accepted code:

class Solution {
public:
string convert(string s, int numRows) {
int len=s.size();
if(numRows<=1||len<=numRows) return s;
vector<string> vs(numRows);
string ss;
int step,row=0;
for(int i=0;i<len;i++)
{
vs[row].push_back(s[i]);
if(row==0) step=1;
else if(row==numRows-1) step=-1;
row+=step;
}

for(int j=0;j<numRows;j++)
ss.append(vs[j]);
return ss;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ZigZag Conversion