1.题目
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200614201740388.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d1cHJvZ3JhbW1lcg==,size_16,color_FFFFFF,t_70)
2.解法(两次翻转法,遍历)
public class Solution {
private char[] arr;
public void reverse(int begin, int end){
char tmp;
while(begin < end){
tmp = arr[begin];
arr[begin] = arr[end];
arr[end] = tmp;
++begin;
--end;
}
System.out.println(arr);
}
public String ReverseSentence(String str) {
if(str == null){
return null;
}
this.arr = str.toCharArray();
reverse(0, arr.length-1);
int start = 0;
for (int i = 0; i <= arr.length; i++) {
if(i == arr.length || arr[i] == ' '){
reverse(start, i-1);
start = i + 1;
}
}
;
return new String(arr);
}
}
时间复杂度为O(n), 空间复杂度为O(n) ,空间复杂度挺高的