6. ZigZag Conversion

来源:互联网 发布:按键精灵淘宝秒杀脚本 编辑:程序博客网 时间:2024/06/05 21:41

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   NA P L S I I GY   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:

  1. P.......A........H.......N
  2. ..A..P....L..S....I...I....G
  3. ....Y.........I........R

Therefore, <ABCD, 2> can be arranged as:

  1. A....C
  2. ...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;    }};


0 0
原创粉丝点击