Created
September 22, 2022 08:35
-
-
Save dluciano/93d9581eefabf38783b3768f110a842f to your computer and use it in GitHub Desktop.
557. Reverse Words in a String III
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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