Why am I getting "Output Limit Exceeded"?


  • 0
    A

    void reverseWords(string &s) {

        int len = 0;
        int startPos = 0;
        string newS;
        
        const char* start = s.c_str();
        const char* end = start + s.size() -1;
        
        if (s.empty())
            return;
        
        do
        {
            len = 0;
            while (*end == ' ' && end != start)
                end--;
            while (*end != ' ' && end != start)
            {
                len++;
                end--;
            }
            
            if (end != start)
            {
                startPos = end - start + 1;
                newS += s.substr(startPos, len);
                newS += " ";
            }
            else
            {
                newS += s.substr(0, len);
            }
            
        }while (end != start);
        
        cout << newS << endl;
        
        return;
    }

Log in to reply
 

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