Clean and straightforward Java solution


  • 0
    public class Solution {
        public String reverseWords(String s) {
            String res = "";
            for(String input: s.split("\\s");) 
                res += reverse(input) + " ";
            return res.substring(0, res.length() - 1);
        }
        
        private static String reverse(String input) {
            char[] chars = new char[input.length()];
            for(int i = 0; i < input.length(); i++) 
                chars[i] = input.charAt(input.length() - 1 - i);
            String res = "";
            for (char ch : chars) res += ch;
            return res;
        }
    }
    

Log in to reply
 

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