557. Reverse Words in a String III

时间:2022-11-20 19:45:12
 static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
string reverseWords(string s)
{
int len=s.length();
int pre=-;
auto p=s.begin();
for(int i=;i<len;i++)
{
if(s[i]==' ')
{
reverse(p+pre+,p+i);
pre=i;
}
}
reverse(p+pre+,s.end());
return s;
}
};

以空格为标志,一段一段逆置