very simple concise solution using java (beats 50%)


  • 0
    V
    public class Solution {
        public String reverseWords(String s) {
            
            String[] words = s.split("\\s+");   // regular expression
            StringBuffer sb = new StringBuffer();
            
            for(int i = words.length - 1; i >= 0; --i){
                sb.append(words[i] + " ");
            }
            
            return sb.toString().trim();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.