Simple Java solution


  • -4
    I
    public void reverseWords(char[] s) {
        String str = new String(s);
        String[] array = str.split(" ");
    
        String reverse = "";
        for (int i = array.length - 1; i >=0; --i) {
            reverse += (array[i] + " ");
        }
    
        reverse = reverse.substring(0, reverse.length() - 1);
    
        System.arraycopy(reverse.toCharArray(), 0, s, 0, s.length);
    }

Log in to reply
 

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