-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path006-ZigZagConversion.cpp
45 lines (37 loc) · 1.13 KB
/
006-ZigZagConversion.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
44
45
//-----------------------------------------------------------------------------
// Runtime: 16ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "006-ZigZagConversion.h"
namespace LeetCode
{
_006_ZigZagConversion::_006_ZigZagConversion()
{
}
_006_ZigZagConversion::~_006_ZigZagConversion()
{
}
string _006_ZigZagConversion::convert(string s, int numRows)
{
if (numRows <= 1 || s.size() <= numRows) { return s; }
string result;
for (int i = 0; i < numRows; i++)
{
for (int j = 0, originalIndex = i;
originalIndex < s.size();
j++, originalIndex = (numRows * 2 - 2) * j + i)
{
result.append(1, s[originalIndex]);
if (i == 0 || i == numRows - 1) { continue; }
originalIndex += (numRows * 2 - 2) - i * 2;
if (originalIndex < s.size())
{
result.append(1, s[originalIndex]);
}
}
}
return result;
}
}