mirror of
https://github.com/mudabbir-ahmad/Leet-Code-prog.git
synced 2026-10-07 18:30:20 +00:00
reverse string sol++
This commit is contained in:
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
|
||||
Reference in new issue
Block a user