LeetCode_557.反转字符串中的单词 III
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
示例:
输入:"Let's take LeetCode contest" 输出:"s'teL ekat edoCteeL tsetnoc"
提示:
- 在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。
C#代码
public class Solution {
public string ReverseWords(string s)
{
StringBuilder builder = new StringBuilder();
string[] midStrList = s.Split(' ');
Stack<char> strStack = new Stack<char>();
for (int i = 0; i < midStrList.Length; i++)
{
string midStr = midStrList[i];
for (int j = 0; j < midStr.Length; j++)
{
strStack.Push(midStr[j]);
}
while (strStack.Any())
{
builder.Append(strStack.Pop());
}
if (i < midStrList.Length - 1)
{
builder.Append(' ');
}
}
return builder.ToString();
}
}