Wrong Answer Input: " " Output: " " Expected: ""


  • 1
    L

    Hi, I need help. Why my program can's handle the input " "?

    The error info:

    Input: " "
    Output: " "
    Expected: ""

    Source Code:

    class Solution{
    public:
    string reverseWords(string &s)
    {
    if(s == "") return "";

    string temp = "";
    string result = "";
    int p, q;
    stack<string> st;
    p = q = s.length()-1;
    
    for(;p > 0; p--)
      {
    while(s[p] == ' ' && p > 0) p--;
    if(p == 0) break;
    q = p;
    while(s[p] != ' ' && p > 0) p--;
    temp = s.substr(p+1, q-p);
    st.push(temp);
    q = p;
    temp.clear();
      }
    
    while(!st.empty())
      {
    result.append(st.top());
    st.pop();
    result.push_back(' ');
      }
    
    if(result == "") return "";
    else
      {
    result.erase(result.end()-1);
    return result;
      }
    

    }
    };


  • 0
    C

    I have met the same problem, I have test by gcc with " " as input, and the run result is "", but still report this error...


Log in to reply
 

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