5 lines C++ using <stringstream>


  • 28
    Z
    void reverseWords(string &s) {
        istringstream is(s);
        string tmp;
        is >> s;
        while(is >> tmp) s = tmp + " " + s;
        if(s[0] == ' ') s = "";
    }

  • 5
    H

    I think it's not in-place code.


  • 0
    C

    This is C++ code, not C


  • 0
    H

    code looks cool, but is it a good practice to access the first element when the string is possibly of length 0 even if it doesn't necessarily trigger seg fault?


  • 3

    Don't understand why so many vote up. It's obviously not what the problem asked. It requires in-place solution. Using temp string is not in-place. And as an interview question, you are not expected to using such library class to solve it. Otherwise, what's the point?


  • 1

    very clear in C++, my in-place C code is posted here.


  • 1

    Just for fun, using C way to solve this.

        //using C way;
    	void reverseWords(string &s) 
        {
            string delimiters = " \f\n\r\t\v";
            s.erase(s.find_last_not_of(delimiters)+1); //removing leading white spaces;
            s.erase(0, s.find_first_not_of(delimiters)); //removing trailing white spaces;
            bool flag = false;
            int index, i;
            for(index = 0, i = 0; s[i]; ++i) //only reserve one white space among words;
            {
                if(s[i] != ' ') s[index++] = s[i], flag = false;
                else
                {
                    if(!flag) s[index++] = s[i];
                    flag = true;
                }
            }
            s.erase(index); //remove left;
            reverse(s.begin(), s.end());
            s += " ";
            int l = 0, r = 0; 
            for(int i = 0; i < s.length(); ++i)
            {
                if(s[i] != ' ') r++;
                else 
                {
                    reverse(s.begin()+l, s.begin()+r);
                    l = ++r; //move to the beginning of the next word;
                }
            }
            s.erase(s.size()-1);
        } 

  • 0

    Same idea here.

    void reverseWords(string &s) {
            istringstream ss(s);
            string temp;
            ss >> temp;
            s = temp;
            while (ss >> temp) s =  temp + ' ' + s;
    }
    

  • 2
    S

    Really short solution! However the following line:

    s = temp + " " + s;
    

    It will keep moving chars backwards every time you insert a new word, which might be close toO(n^2).
    What about a longer solution:

        void reverseWords(string &s) {
            istringstream in(s);
            string word;
            s = "";
            while (in >> word) {
                reverse(word.begin(), word.end());
                s += word + " ";
            }
            if (!s.empty()) {
                s.pop_back();
                reverse(s.begin(), s.end());
            }
        }
    

  • 1

    @Scar Indeed, you are quite right in this. But I do think the readability and tidiness are quite important here. Besides the performance usually doesn't suffer too much from this kind of movement.


  • 0
    S

    @LHearen Yeah, totally agree with you. If the string is not so long, I think that the breif solution is better.


  • 0
    M

    What is last line used for:

    if(s[0] == ' ') s = "";

    how does this work with istringstream and >> operator?

    Thank you!


  • 1

    @cpanleetcode You may want to check this post first. >> will check the string of the original s if it's valid, which means istringstream can read from it then it will return true otherwise false. So here " " will be valid for a string but tmp will be "" since it's space only but s then will be " " according to s=tmp+" "+s, so in this case, according to the OJ, we should set s to be "" instead of " ". So at last, we have the last statement for this case.


  • 0
    O

    like it! temp+s not s+temp really make me shocked,brilliant


  • 0
    Y

    runtime 6ms O(n) and memory O(n) solution is here


Log in to reply
 

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