reverse string sol++

This commit is contained in:
bobbert committed 2026-05-04 20:06:06 +01:00
1 parent 615bb68cc9
commit e4d9f66987
2 files changed
+1

No files matched your search

@@ -0,0 +1,27 @@
class Solution {
public:
string reverseWords(string s) {
reverse(s.begin(), s.end());
int n = s.size();
int left = 0;
int right = 0;
int i = 0;
while (i < n) {
while (i < n && s[i] == ' ')
i++;
if (i == n)
break;
while (i < n && s[i] != ' ') {
s[right++] = s[i++];
}
reverse(s.begin() + left, s.begin() + right);
s[right++] = ' ';
left = right;
i++;
}
s.resize(right - 1);
return s;
}
};
// Makes a reverse of the string whilst keeping word spelling the same