-
Notifications
You must be signed in to change notification settings - Fork 115
/
006-ZigZagConversion.cs
37 lines (32 loc) · 1.11 KB
/
006-ZigZagConversion.cs
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
//-----------------------------------------------------------------------------
// Runtime: 148ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _006_ZigZagConversion
{
public string Convert(string s, int numRows)
{
if (numRows <= 1 || s.Length <= 1) { return s; }
var result = new char[s.Length];
var index = 0;
for (int i = 0; i < numRows; i++)
{
for (int j = 0; (numRows * 2 - 2) * j + i < s.Length; j++)
{
var originalIndex = (numRows * 2 - 2) * j + i;
result[index++] = s[originalIndex];
if (i == 0 || i == numRows - 1) { continue; }
originalIndex = originalIndex + (numRows * 2 - 2) - i * 2;
if (originalIndex < s.Length)
{
result[index++] = s[originalIndex];
}
}
}
return new string(result);
}
}
}