废话少说直接上代码
static string Reveres(string s)
{
char[] target = new char[s.Length];
int index = 0;
int offset = 0;
for (int i = 0; i < s.Length; i++)
{
jump:
if (offset == i)
{
for (int y = offset + 1; y < s.Length; y++)
{
if (s[y] == ' ')
{
offset = y;
target[offset] = ' ';
index = offset - 1;
goto jump;
}
}
offset = -1;
index = s.Length - 1;
}
if (s[i] == ' ') continue;
target[index] = s[i];
target[i] = s[index];
index--;
if (index <= i && offset > -1)
{
i = offset;
goto jump;
}
}
return new string(target);
}
讲一下思路 优先考虑的是减少遍历
所以走的是双向赋值
纯手工算的