1 static int wing=[]() 2 { 3 std::ios::sync_with_stdio(false); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Solution 9 { 10 public: 11 string reverseWords(string s) 12 { 13 int len=s.length(); 14 int pre=-1; 15 auto p=s.begin(); 16 for(int i=0;i<len;i++) 17 { 18 if(s[i]==' ') 19 { 20 reverse(p+pre+1,p+i); 21 pre=i; 22 } 23 } 24 reverse(p+pre+1,s.end()); 25 return s; 26 } 27 };
以空格为标志,一段一段逆置