这道题目貌似我在ACM的时候见过,直接正常逻辑写出来就可以了
public class Solution {
public String ReverseSentence(String str) {
char[] str1 = str.toCharArray();
int len = str.length();
if(len==0)
return "";
char m = str1[0];
char[] str2 = new char[len];
int mark = 0;
for(int i=len-1;i>=-1;i--){
int j=i;
while(str1[i]!=' '&&str1[i]!=m){
i=i-1;
}
if(i==0)
i=i-1;
for(int k=i+1;k<=j;k++){
str2[mark++] = str1[k];
if(k==j&&i!=-1){
str2[mark++] = ' ';
}
}
if(i==-1)
break;
}
String str3 = new String(str2);
return str3;
}
}