Skip to content

Commit 3ac56d8

Browse files
committed
https://leetcode.com/problems/reverse-words-in-a-string/
1 parent 1a505a0 commit 3ac56d8

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
namespace LeetCode.MediumProblems
22
{
3-
public class Reverse_Words_in_a_String
3+
/// <summary>
4+
/// https://leetcode.com/problems/reverse-words-in-a-string/
5+
/// </summary>
6+
public class ReverseWordsInAString
47
{
58
public string ReverseWords(string s)
69
{
7-
var wordArray = s.Split(" ").Where(x => !string.IsNullOrEmpty(x)).ToArray();
8-
string returnValue = "";
9-
for (int i = wordArray.Length - 1; i >= 0; i--)
10-
{
11-
returnValue += $"{wordArray[i].Trim()} ";
12-
}
13-
14-
return returnValue.TrimEnd();
10+
string[] words = s.Split(' ', StringSplitOptions.RemoveEmptyEntries
11+
| StringSplitOptions.TrimEntries);
12+
Array.Reverse(words);
13+
return string.Join(" ", words);
1514
}
1615
}
17-
}
16+
}

0 commit comments

Comments
 (0)