My accepted Answer in Java


  • 1
    I
    public class Solution {
    public String reverseWords(String s) {
        String result = "";
        String[] splitStr = s.split(" ");
        for(String str : splitStr) {
            if(str.length() !=0)
                result = str + " " + result;
        }
        return result.trim();
    }
    

    }


  • 0
    S
    1. What if there are several spaces between 2 words.
    2. why will you judge if str.length(), I think it is meaningless, what we should know is when the array ends

  • 0
    A
    This post is deleted!

Log in to reply
 

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