-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cs
41 lines (38 loc) · 937 Bytes
/
Solution.cs
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
34
35
36
37
38
39
40
41
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public string ReverseWords(string s) {
var sb = new StringBuilder();
var wordList = new List<string>();
foreach (var ch in s)
{
if (char.IsWhiteSpace(ch))
{
if (sb.Length > 0)
{
wordList.Add(sb.ToString());
sb.Clear();
}
}
else
{
sb.Append(ch);
}
}
if (sb.Length > 0)
{
wordList.Add(sb.ToString());
sb.Clear();
}
foreach (var word in ((IEnumerable<string>)wordList).Reverse())
{
if (sb.Length > 0)
{
sb.Append(' ');
}
sb.Append(word);
}
return sb.ToString();
}
}