public class Solution { public String ReverseSentence(String str) { if(str==null||str.trim().length()==0) return str; //空格分隔转字符串数组 String[] arr = str.split(" "); StringBuilder sb = new StringBuilder(); //数组逆向拼接字符串,中间加空格 for(int i=0;i<arr.length;i++){ sb.append(arr[arr.length-1-i]+" "); } //去除尾部空格转字符串输出 return sb.deleteCharAt(sb.length()-1).toString(); } }