Skip to content

Instantly share code, notes, and snippets.

@dluciano
Created September 22, 2022 08:35
Show Gist options
  • Save dluciano/93d9581eefabf38783b3768f110a842f to your computer and use it in GitHub Desktop.
Save dluciano/93d9581eefabf38783b3768f110a842f to your computer and use it in GitHub Desktop.
557. Reverse Words in a String III
public class Solution {
public string ReverseWords(string s) {
var chars = s.ToCharArray();
void Reverse(char[] word, int left, int right){
for(; left < right; ++left, --right)
(word[left], word[right]) = (word[right], word[left]);
}
int left = 0, right = 0;
while(right < chars.Length){
if(chars[right] != ' ') {
right++;
continue;
}
Reverse(chars, left, right - 1);
++right;
left = right;
}
Reverse(chars, left, right - 1);
return new string(chars);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment