public class Solution {
//不开新的数组的情况下
//翻转两次 即可
//1. 先翻转整个字符串
//2. 再反转每个单词
public String ReverseSentence(String str) {
str = reverse(str, 0, str.length() - 1);
int i = 0, j = 0;
for (; i < str.length(); i++) {
if(str.charAt(i) == ' ') {
str = reverse(str, j, i - 1);
j = i + 1;
}
}
str = reverse(str, j, str.length() - 1);
return str;
}
public String reverse(String str, int start, int end) {
char[] strs = str.toCharArray();
int i = start, j = end;
for (; i <= j; i++, j--) {
char c = strs[i];
strs[i] = strs[j];
strs[j] = c;
}
return String.valueOf(strs);
}
}