forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseWordsInAString.java
35 lines (32 loc) · 952 Bytes
/
ReverseWordsInAString.java
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
// Source : https://leetcode.com/problems/reverse-words-in-a-string/description/
// Author : Tianming Cao
// Date : 2018-02-11
/**********************************************************************************
*
* Given an input string, reverse the string word by word.
*
* For example,
* Given s = "the sky is blue",
* return "blue is sky the".
*
* Update (2015-02-12):
* For C programmers: Try to solve it in-place in O(1) space.
*
**********************************************************************************/
package reverseWordsInAString;
public class ReverseWordsInAString {
public String reverseWords(String s) {
if (s == null || s.length() == 0) {
return s;
}
s = s.trim();
String[] array = s.split("\\s+");
StringBuilder sb = new StringBuilder();
int len = array.length;
for (int i = len - 1; i > 0; i--) {
sb.append(array[i]).append(" ");
}
sb.append(array[0]);
return sb.toString();
}
}