[Java]: Easy to understand, using StringBuilder()


  • 0
    S
        public String reverseWords(String s) {
            StringBuilder word = null;
            StringBuilder sentence = new StringBuilder();
            String[] words = s.split("\\s");
    
            for(int i = 0; i < words.length; i++){
                word = new StringBuilder(words[i]).reverse();
                if(i != words.length - 1) sentence.append(word + " ");
                else sentence.append(word);
            }
            return sentence.toString();
        }
    

Log in to reply
 

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