-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode006.cpp
42 lines (40 loc) · 1.04 KB
/
leetcode006.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
/*************************************************************************
> File Name: leetcode006.cpp
> Author:
> Mail:
> Created Time: Wed 21 Sep 2016 12:12:08 AM PDT
************************************************************************/
#include<iostream>
using namespace std;
string convert(string s, int numRows) {
if(numRows == 1 || s.length() <= numRows)
return s;
string result = "";
for(int i = 0 ; i < numRows;i++)
{
int j = i;
int step = numRows + numRows - 2;
int k = step - i;
while(j < s.length() )
{
result +=s[j];
j += step;
if(!(i == 0 || i == numRows - 1))
{
if(k < s.length())
{
result +=s[k];
k += step;
}
}
}
}
return result;
}
int main()
{
cout << convert("PAYPALISHIRING", 3) <<endl;
cout << convert("P", 1) <<endl;
cout << convert("abcdefghijkl", 4) <<endl;
cout << convert("abcde", 4) <<endl;
}