題目:Reverse Words in a String
難度:Medium
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class Solution { public string ReverseWords(string s) { string[] words = s.Split(' '); string[] filteredWords = words.Where(i => String.IsNullOrEmpty(i) == false).ToArray(); List<string> reversedWords = new List<string>();
for (int i = filteredWords.Count() - 1; i >= 0; i--) { reversedWords.Add(filteredWords[i]); }
string ans = String.Join(" ", reversedWords);
return ans; } }
|