0%

6. ZigZag Conversion

6. ZigZag Conversion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Solution
{
public:
string convert(string s, int numRows)
{
int size = s.size();
if (numRows == 1)
{
return s;
}
int col = size / (numRows + numRows - 2);
int addon = size - col * (numRows + numRows - 2);
string result = "";
int location = 0;
for (size_t i = 0; i < numRows; ++i)
{
int bias;
if (i == numRows - 1)
{
int temp = col + (addon >= numRows ? 1 : 0);
for (int i = 0; i < temp; ++i)
{
result += string(1, s[location + i*(numRows+numRows-2)]);
}
++location;
continue;
}
else if (i == 0)
{
int temp = col + (addon ? 1 : 0);
for (int i = 0; i < temp; ++i)
{
result += string(1, s[location + i * (numRows + numRows - 2)]);
}
++location;
continue;
}
else if (addon >= 2 * numRows - 2 - (i - 1))
{
bias = 2;
}
else if (addon >= i + 1)
{
bias = 1;
}
else
{
bias = 0;
}
int temp = col + col + bias;
for (int i = 0; i < temp; ++i)
{
if(i%2==0)
result += string(1, s[location + (i/2) * (numRows + numRows - 2)]);
else
result += string(1, s[location + (i/2) * (numRows + numRows - 2)+2*numRows-4-2*(location-1)]);
}
++location;
}
return result;
}
};
Solution中的两种方法
按行排列
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
string convert(string s, int numRows) {
if(numRows == 1)
return s;
vector<string> r(min(numRows, (int)(s.size())));
int curRow = 0;
bool goDown = true;
for(auto ch : s)
{
r[curRow] += ch;
curRow += goDown ? 1 : -1;
if(curRow == 0 || curRow == numRows - 1)
goDown = !goDown;
}
string ret;
for(auto& str : r)
ret += str;
return ret;
}
};
按行访问

找到每行的字母在s中字母位置和他行数之间的关系