-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy path0541-ReverseStringII.cs
More file actions
33 lines (29 loc) · 932 Bytes
/
0541-ReverseStringII.cs
File metadata and controls
33 lines (29 loc) · 932 Bytes
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
//-----------------------------------------------------------------------------
// Runtime: 84ms
// Memory Usage: 24.9 MB
// Link: https://leetcode.com/submissions/detail/351890985/
//-----------------------------------------------------------------------------
using System;
using System.Text;
namespace LeetCode
{
public class _0541_ReverseStringII
{
public string ReverseStr(string s, int k)
{
if (string.IsNullOrEmpty(s) || s.Length == 1) return s;
var sb = new StringBuilder(s);
for (int i = 0; i < s.Length; i += 2 * k)
{
int start = i, end = Math.Min(i + k, s.Length) - 1;
while (start < end)
{
var temp = s[start];
sb[start++] = sb[end];
sb[end--] = temp;
}
}
return sb.ToString();
}
}
}