public class Solution { public string ReverseWords(string s) { var list = s.Split(' ').AsEnumerable().ToList();//用空格将单词分隔开 for (int i = 0; i < list.Count(); i++)//循环每一个单词 { var str = list[i]; var chars = str.Reverse();//将这个单词反转 StringBuilder sb = new StringBuilder(); foreach (var c in chars) { sb.Append(c); } list[i] = sb.ToString();//存储反转后的单词 } StringBuilder sb2 = new StringBuilder(); foreach (var str in list)//拼接单词 { sb2.Append(str); sb2.Append(" "); } var result = sb2.ToString().Substring(0, sb2.Length - 1);//消去最后的一个空格 //Console.WriteLine(result); return result; } }
https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description