http://oj.leetcode.com/problems/reverse-words-in-a-string/

/*48ms*/

class Solution
{
    public:
        char s[1000005];
        vector<string> ans;
        
        void split(const char *delimiters)
        {
            ans.clear();
            for (char *p = strtok(s, delimiters); p; p = strtok(NULL, delimiters))
                ans.push_back(p);
        }

        void reverseWords(string &ss)
        {
            strcpy(s, ss.c_str());
            split(" ");
            if (ans.size() == 0) ss = "";
            else
            {
                ss = ans[ans.size() - 1];
                for (int i = ans.size() - 2; i >= 0; --i) ss += " " + ans[i];
            }
        }
};

Comments

comments powered by Disqus