# [LeetCode]ZigZag Conversion

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

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 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 解题报告

2014-10-26 16:49:26

#### Leetcode_6_ZigZag Conversion

2014-11-23 09:45:55

#### [Java]leetcode6 ZigZag Conversion

2015-04-12 11:17:26

#### 【LeetCode】LeetCode——第6题：ZigZag Conversion

2016-04-19 10:09:57

#### [LeetCode][6]ZigZag Conversion解析与特殊解法 -Java实现

2016-06-06 14:29:53

#### LeetCode::Zigzag Conversion C语言

2015-03-07 22:36:11

#### [Leetcode]6. ZigZag Conversion @python

2016-01-13 00:32:16

#### leetcode 6 ZigZag Conversion C++

2016-06-28 10:52:31

#### LeetCode-6. ZigZag Conversion (JAVA)（之字形转换）

2017-04-17 09:41:03

#### [LeetCode] 006. ZigZag Conversion (Easy) (C++/Java/Python)

2015-02-28 00:13:47