forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
38 lines (35 loc) · 1.1 KB
/
Solution.cs
File metadata and controls
38 lines (35 loc) · 1.1 KB
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
namespace LeetCodeNet.G0001_0100.S0006_zigzag_conversion {
// #Medium #String #Top_Interview_150_Array/String #Big_O_Time_O(n)_Space_O(n)
// #2025_06_12_Time_3_ms_(95.39%)_Space_46.59_MB_(85.85%)
using System.Text;
public class Solution {
public string Convert(string s, int numRows) {
int sLen = s.Length;
if (numRows == 1) {
return s;
}
int maxDist = numRows * 2 - 2;
StringBuilder buf = new StringBuilder();
for (int i = 0; i < numRows; i++) {
int index = i;
if (i == 0 || i == numRows - 1) {
while (index < sLen) {
buf.Append(s[index]);
index += maxDist;
}
} else {
while (index < sLen) {
buf.Append(s[index]);
index += maxDist - i * 2;
if (index >= sLen) {
break;
}
buf.Append(s[index]);
index += i * 2;
}
}
}
return buf.ToString();
}
}
}