public class Solution { public String reverseWords(String s) { if(s==null||s.length()==0) return ""; s=reverse(s); String s2[]=s.split("\\s+"); StringBuffer sbf=new StringBuffer(); for(int i=0;i<s2.length;i++) { sbf.append(reverse(s2[i])+" "); } return sbf.toString().trim(); } public String reverse(String s) { char c[]=s.toCharArray(); for(int i=0;i<c.length/2;i++) { char t=c[i]; c[i]=c[s.length()-i-1]; c[s.length()-i-1]=t; } return String.valueOf(c); } }
时间: 2024-10-29 19:05:37