import java.util.*; public class Solution { public String ReverseSentence(String str) { if(str==null||str.length()==0){ return ""; }if(str.trim().equals("")){ return str; } String[] strings = str.split(" "); List<String> list = new ArrayList<>(); for(int i = strings.length - 1;i>=0;i--){ list.add(strings[i]); } StringBuffer stringBuffer = new StringBuffer(); for(int i =0;i<list.size();i++){ if(i<list.size()-1){ stringBuffer.append(list.get(i)+" "); }else{ stringBuffer.append(list.get(i)); } } return stringBuffer.toString(); } }
翻转单词顺序列,如“student. a am I”,返回的的句子应该是“I am a student.”。
原文:https://www.cnblogs.com/q-1993/p/10918090.html