ZigZag Conversion

来源:互联网 发布:腾讯云 centos 7 pptp 编辑:程序博客网 时间:2024/06/02 07:24

【题目】      

     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"

【题意】  

      把字符串按照"Z"形重新排列,再按行链接起来输出 

分析

      ZigZag是把字符串原顺序012345……按下图所示排列:


比较直观的解法是,用一个字符串数组 string[rows] 来存储每一行,最后一拼接就是最终结果。

【实现】

public class Solution {    public String convert(String s, int nRows) {        int len = s.length();        if (len == 0 || nRows <= 1) return s;                String[] ans = new String[nRows];        Arrays.fill(ans, "");        int row = 0, delta = 1;        for (int i = 0; i < len; i++) {            ans[row] += s.charAt(i);            row += delta;            if (row >= nRows) {                row = nRows-2;                delta = -1;            }            if (row < 0) {                row = 1;                delta = 1;            }        }                String ret = "";        for (int i = 0; i < nRows; i++) {            ret += ans[i];        }        return ret;    }}
另一种解:

通过画图观察,我们可以发现,

 第0行和最后一行中,前一个下标的值和后一个下标的值相差 2 * nRows - 2 ,以第0行为例,前一个下标为0,

 后一个下标为 0+2*4-2=6。

 中间行中,前一个下标的值和后一个下标的值需要根据这个下标是该行中的奇数列还是偶数列来计算。以平时的习惯

 来计算,因此,行和列的开始值都是0。

 以第2行为例,第一个下标是2,后一个下标所处列为1,是奇数列,因此从这个下标到下一个下标相差的值是它们所处

的行i下面的所有行的点的个数,即2 * (nRows - 1 - i)。在这里,nRows-1是为了遵循0下标开始的原则。这样,我们可以

求得这个奇数列的下标为 2+2*(4-1-2)=4。同理,当我们要计算4之后的下标时,我们发现下一个所处的是偶数列2,从这个

下标到下一个下标相差的值其实是它们所处的行i上面的所有行的点的个数,即2 * i。因此,该列的下标计算为4+2*2=8。

 

有了以上的公式,我们就可以写出代码了。但需要注意几点:

1.判断所求出的下标是不是越界了,即不能超出原字串的长度;

2.注意处理nRows=1的情况,因为它会使差值2 * nRows - 2的值为0,这样下标值会一直不变,进入死循环,产生

    Memory Limit Exceeded 或者 Output Limit Exceeded之类的错误。

3.还有一些其他的边界情况,如:nRows > s.length,s.length = 0等需要处理。

【实现】

class Solution {public:    string convert(string s, int nRows) {        // IMPORTANT: Please reset any member data you declared, as        // the same Solution instance will be reused for each test case.                  if (nRows <= 1 || s.length() == 0)           return s;        string res = "";        int len = s.length();        for (int i = 0; i < len && i < nRows; ++i)        {int indx = i;res += s[indx];            for (int k = 1; indx < len; ++k)            {                //第一行或最后一行,使用公式1:                if (i == 0 || i == nRows - 1)                {                    indx += 2 * nRows - 2;                }                //中间行,判断奇偶,使用公式2或3                else                {                    if (k & 0x1)  //奇数位indx += 2 * (nRows - 1 - i);                    else indx += 2 * i;                }                //判断indx合法性                if (indx < len)                {                    res += s[indx];                }            }        }        return res;    }};

public class Solution {    public String convert(String s, int nRows) {        int len = s.length();        if (len == 0 || nRows < 2) return s;                String ret = "";        int lag = 2*nRows - 2; //循环周期        for (int i = 0; i < nRows; i++) {            for (int j = i; j < len; j += lag) {                ret += s.charAt(j);                                //非首行和末行时还要加一个                if (i > 0 && i < nRows-1) {                    int t = j + lag - 2*i;                    if (t < len) {                        ret += s.charAt(t);                    }                }            }        }        return ret;    }}



0 0
原创粉丝点击