Reverse words in a String leetcode

来源:互联网 发布:2016淘宝联盟使用教程 编辑:程序博客网 时间:2024/05/17 08:21
class Solution {public:    void reverseWords(string &s) {        vector<string> temp;        string str;        for(int i=0;i<s.length();i++)        {            if(s[i]!=' ')            {                str=str+s[i];            }            else            {                if(str.size()!=0)                {                    temp.push_back(str);                    str.clear();                }            }        }if(str.size()!=0)temp.push_back(str);        s.clear();        for(int i=0;i<temp.size();i++)        {            s=s+temp[temp.size()-i-1];            if(i!=temp.size()-1)               s=s+' ';        }    }};
不用脑子的办法
0 0