class Solution { public: string reverseWords(string s) { int n=s.size(); int l=0,r=0; while(l<n) { while(r<n && s[r]!=' ') { r++; } reverse(s.begin()+l,s.begin()+r); l=r+1; r=l; } return s; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter