publicclassSolution{ public String reverseWords(String s){ if (s == null || s.length() == 0){return"";} StringBuilder sb = new StringBuilder(); int end = s.length(); for (int i = s.length() - 1; i >= 0; i--){ if (s.charAt(i) == ' '){ if (end - i > 1){ sb.append(s.substring(i + 1, end)); sb.append(" "); } end = i; }else{ if (i == 0){sb.append(s.substring(i, end));} } } return sb.toString().trim(); } }