C# solution beats 98%


  • -1
    P

    public string ReverseWords(string str)
    {
    StringBuilder returnString = new StringBuilder();
    str = str.Trim();
    string[] temp = str.Split(null);
    for (int i = temp.Length - 1; i >= 0; i--) {
    if(!string.IsNullOrEmpty(temp[i]))
    returnString.Append(temp[i].Trim()).Append(" ");
    }
    return returnString.ToString().Trim();
    }
    // the above trim() method calls are to be extra sure about avoiding trailing/leading spaces


Log in to reply
 

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