Run time error, how can I improve it?


  • 0
    L

    class Solution {
    public:
    void reverseWords(string &s) {
    findWords(s);
    }

    void findWords(string &s) {
        string temp;
        stack <string> words;
        int str_len = s.length();
        int bg = 0, ed = str_len;
        while(1){
            findWord(s, bg, ed);
            if(bg < 0)
                break;
            if(ed > bg)
                temp.assign(s, bg, ed-bg);
            else{
                temp.assign(s, bg, str_len);
                cout << temp << temp.length() << endl;
                break;
            }
            words.push(temp);
            bg = ed;
            ed = str_len;
        }
        while(words.size() - 1){
            cout << words.top() <<" ";
            words.pop();
        }
            cout << words.top();
    }
    
    void findWord(string &s, int &bg, int &ed)
    {
        bg = s.find_first_not_of(' ', bg);
        ed = s.find_first_of(' ', bg);
    }
    

    };


Log in to reply
 

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