![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7b91dacfb129a0da2a9ded21de5a0566.png)
class Solution {
public String reverseWords(String s) {
char[] s2CharArr = s.toCharArray();
int length = s2CharArr.length;
int[] startPos = new int[length];
int startPosIndex = 0;
int[] endPos = new int[length];
int endPosIndex = 0;
for(int i = 0; i < length; i++){
if((i == 0 || s2CharArr[i-1] == ' ') && s2CharArr[i] != ' '){
startPos[startPosIndex] = i;
startPosIndex += 1;
}
if((i == length-1 || s2CharArr[i+1] == ' ') && s2CharArr[i] != ' '){
endPos[endPosIndex] = i;
endPosIndex += 1;
}
}
String result = null;
for(int j = 0;j < startPos.length;j++){
reverseMethod(s2CharArr,startPos[j],endPos[j]);
}
return new String(s2CharArr);
}
public void reverseMethod(char[] s2CharArr,int start,int end){
while(start < end){
char temp = ' ';
temp = s2CharArr[start];
s2CharArr[start] = s2CharArr[end];
s2CharArr[end] = temp;
start ++;
end --;
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ee72bea98690f750f942d75895ba3285.png)