forked from haoel/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseWordsInAString.java
More file actions
Latest commit
35 lines (32 loc) · 952 Bytes
/
Copy pathReverseWordsInAString.java
File metadata and controls
35 lines (32 loc) · 952 Bytes
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.
*
**********************************************************************************/
packagereverseWordsInAString;
publicclassReverseWordsInAString {
publicStringreverseWords(Strings) {
if (s == null || s.length() == 0) {
returns;
}
s = s.trim();
String[] array = s.split("\\s+");
StringBuildersb = newStringBuilder();
intlen = array.length;
for (inti = len - 1; i > 0; i--) {
sb.append(array[i]).append(" ");
}
sb.append(array[0]);
returnsb.toString();
}
}