-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path006.cpp
43 lines (42 loc) · 1.06 KB
/
006.cpp
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
class Solution {
public:
string convert(string s, int numRows) {
// 模拟法
int len = s.length(), shang = 2*numRows -2;
// 特殊情况处理
if(len == 0) return "";
if(numRows == 1)
return s;
int count = len/shang + 1;
string res;
int th = 0;
// 第一排
for(int i=0, th=0;i<count;++i)
{
th = 0 + i*shang;
if(th < len)
res.push_back(s[th]);
}
// 第2-(numRows-1)排
for(int i=1;i<numRows-1;++i)
{
for(int j=0;j<count;++j)
{
th = i + j*shang;
if(th < len)
res.push_back(s[th]);
th = shang - i + j*shang;
if(th < len)
res.push_back(s[th]);
}
}
// 第numRows-1排
for(int i=0;i<count;++i)
{
th = numRows-1 + i*shang;
if(th < len)
res.push_back(s[th]);
}
return res;
}
};