Example 1:
Input: “the sky is blue”
Output: “blue is sky the”
Example 2:
Input: " hello world! "
Output: “world! hello”
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: “a good example”
Output: “example good a”
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
Note:
A word is defined as a sequence of non-space characters.
Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
You need to reduce multiple spaces between two words to a single space in the reversed string.
Follow up:
For C programmers, try to solve it in-place in O(1) extra space.
思路一: 暴力解决
if (s == null || s.length() == 0) return s;
char[] a = s.toCharArray();
int count = 0;
int i, j;
for (i = 0; i < a.length && a[i] == ' '; ++i);
for ( ; i < a.length; ++i) {
if (i == 0 || !(a[i] == ' ' && a[i - 1] == ' '))
a[count++] = a[i];
}
if (count != 0 && a[count - 1] == ' ') count -= 1;
char[] b = new char[count];
for (i = 0; i < count; ++i) b[i] = a[i];
a = b;
i = 0; j = a.length - 1;
while (i < j) {
char t = a[i];
a[i++] = a[j];
a[j--] = t;
}
for (i = 0; i < a.length; ++i) {
if (a[i] != ' ') {
for (j = i + 1; j < a.length && a[j] != ' '; ++j) ;
int last = j;
j -= 1;
while (i < j) {
char t = a[i];
a[i++] = a[j];
a[j--] = t;
}
i = last;
}
}
return new String(a);
思路2 灵活运用java的API分割字符串
public String reverseWords(String s) {
final String[] words = s.trim().split(" ");
final StringBuilder result = new StringBuilder();
for (int index = words.length - 1; index >= 0; index--) {
if (!words[index].isEmpty()) {
result.append(words[index])
.append(" ");
}
}
return result.toString().trim();
}
思路3
/*public String reverseWords(String s) {
if (s == null) return null;
char[] a = s.toCharArray();
int n = a.length;
// step 1. reverse the whole string
reverse(a, 0, n - 1);
// step 2. reverse each word
reverseWords(a, n);
// step 3. clean up spaces
return cleanSpaces(a, n);
}
public void reverseWords(char[] a, int n) {
int i = 0, j = 0;
while (i < n) {
while (i < j || i < n && a[i] == ' ') i++; // skip spaces
while (j < i || j < n && a[j] != ' ') j++; // skip non spaces
reverse(a, i, j - 1); // reverse the word
}
}
// trim leading, trailing and multiple spaces
public String cleanSpaces(char[] a, int n) {
int i = 0, j = 0;
while (j < n) {
while (j < n && a[j] == ' ') j++; // skip spaces
while (j < n && a[j] != ' ') a[i++] = a[j++]; // keep non spaces
while (j < n && a[j] == ' ') j++; // skip spaces
if (j < n) a[i++] = ' '; // keep only one space
}
return new String(a).substring(0, i);
}
// reverse a[] from a[i] to a[j]
private void reverse(char[] a, int i, int j) {
while (i < j) {
char t = a[i];
a[i++] = a[j];
a[j--] = t;
}
}*/
/*public String reverseWords(String s) {
if(s==null)
return null;
s = s.trim();
if(s.isEmpty())
return s;
StringBuffer sb = new StringBuffer();
String[] tokens = s.split(" ");
for(int i=tokens.length-1;i>0;i--){
if(!tokens[i].isEmpty()){
sb.append(tokens[i]);
sb.append(" ");
}
}
sb.append(tokens[0]);
return sb.toString();
}*/
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder();
int i = s.length() - 1;
while(i >= 0){
if(s.charAt(i) == ' '){
i--;
continue;
}
int start = s.lastIndexOf(' ', i);
sb.append(' ');
sb.append(s.substring(start + 1, i + 1));
i = start - 1;
}
if(sb.length() > 0){
sb.deleteCharAt(0);
}
return sb.toString();
}