[LeetCode]ZigZag Conversion

原创 2015年07月08日 17:55:16

虽然是easy的模式,但是还是写了好久。。
0 P H
1 A G I
i B F J
i+1 C E K
n-1 D L

直接以字符的下标来处理比较简单
关键是计算间隔
H 和P以及L和D之间的间隔是2(n-1)

F和B的间隔是2(n-1-i)
J和B的间隔也是2(n-1)或者是2(n-1-i)+2*i

循环的出口时下标>=len

class Solution {
public:
    string convert(string s, int numRows) {
        int len = s.size();
        string out;
        int idx;
        if(len ==1||numRows==1) return s;
        for(int i = 0; i != numRows;++i){
            idx = i;
            if(idx==0||idx == numRows-1){
                while(idx<len) {out.append(1,s[idx]); idx = idx + 2*(numRows -1);}
            }
            else{
                while(idx<len){
                    out.append(1,s[idx]);
                    idx = idx + 2*(numRows-1-i); 
                    if(idx>=len) break;
                    else{
                        out.append(1,s[idx]);
                        idx = idx + 2*i;
                    }

                }
            }

        }

        return out;


    }
};

有个神奇的代码
使用string数组或者vector大小为numRows,在字符下标i==0时,row向下生长,当字符下标j==numRows时,向上生长。

string convert(string s, int nRows) {

    if (nRows <= 1)
        return s;

    const int len = (int)s.length();
    string *str = new string[nRows];

    int row = 0, step = 1;
    for (int i = 0; i < len; ++i)
    {
        str[row].push_back(s[i]);

        if (row == 0)
            step = 1;
        else if (row == nRows - 1)
            step = -1;

        row += step;
    }

    s.clear();
    for (int j = 0; j < nRows; ++j)
    {
        s.append(str[j]);
    }

    delete[] str;
    return s;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[LeetCode] ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (y...

leetcode: ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (y...

[Java]leetcode6 ZigZag Conversion

[Java]leetcode ZigZag Conversion
  • fumier
  • fumier
  • 2015年04月12日 11:17
  • 564

[LeetCode - 字符串] 6. ZigZag Conversion

1 问题 The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like thi...

leetcode-------------ZigZag Conversion

题目:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: ...

LeetCode 6 ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (y...

【LeetCode】006.ZigZag Conversion - javascript

ZigZag转换,是什么意思?这个是困扰大家解题的关键。本质就是Z字形转换:将字符串换Z字排好,或者是倒“N”型排好;然后再按行重新组成新的字符串。...

leetcode之路006 ZigZag Conversion

题目大意:给定一个以之字形形式存储的字符串,并且给定之字形的行数,如下,行数为3,给定的string为"PAYPALISHIRING" P A H N A P L S I I G Y ...

LeetCode刷题(C++)——ZigZag Conversion(Medium)

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (yo...

Leetcode 6. ZigZag Conversion The Solution of Python

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[LeetCode]ZigZag Conversion
举报原因:
原因补充:

(最多只允许输入30个字)