Share my Java simple sol


  • 0
    J
    public String reverseWords(String s) {
        s = s.trim();
        String[] sc = s.split(" +");
        if(sc.length==0){
            return new String("");
        }
        String sr = " ";
        for(int i=sc.length-1;i>=0;i--){
            sr = sr+sc[i]+" "; 
        }
        return sr.trim();
    }

  • 0
    A

    Question asks for O(1) space complexity.


Log in to reply
 

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