更新时间:2022-10-02 22:47:52
6. ZigZag Conversion
题目:https://leetcode.com/problems/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
|
string convert2(string s, int numRows) {
if (s.length() < 2 || numRows < 2)
return s;
int cycle = 2 * numRows - 2;
string tmp;
string result;
for ( int i = 0; i < numRows; i++)
{
if (i == 0 || i == numRows - 1)
{
for ( int j = 0; j < s.length(); j += cycle)
{
if (i + j < s.length())
tmp += s.at(i+j);
}
result += tmp;
cout << tmp << endl;
tmp.clear();
}
else
{
int minus = numRows - i - 1;
int j, j1;
bool find = false ;
for (j = i, j1 = 0; j < s.length(); j += cycle, j1 += cycle)
{
tmp += s.at(j);
if (!find)
{
j1 = j + 2 * minus;
}
if (j1 < s.length())
tmp += s.at(j1);
}
result += tmp;
cout << tmp << endl;
tmp.clear();
}
}
return result;
} |
2016-08-08 20:30:52
本文转自313119992 51CTO博客,原文链接:http://blog.51cto.com/qiaopeng688/1835791