Advertisement
sweet1cris

Untitled

Jan 9th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. public class Solution {
  2.     public String reverseWords(String s) {
  3.         if (s == null || s.length() == 0) {
  4.             return "";
  5.         }
  6.  
  7.         String[] array = s.split(" ");
  8.         StringBuilder sb = new StringBuilder();
  9.  
  10.         for (int i = array.length - 1; i >= 0; --i) {
  11.             if (!array[i].equals("")) {
  12.                 sb.append(array[i]).append(" ");
  13.             }
  14.         }
  15.  
  16.         //remove the last " "
  17.         return sb.length() == 0 ? "" : sb.substring(0, sb.length() - 1);
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement