Skip to content

Instantly share code, notes, and snippets.

@xynophon
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save xynophon/9507d51a3d1f33242d59 to your computer and use it in GitHub Desktop.

Select an option

Save xynophon/9507d51a3d1f33242d59 to your computer and use it in GitHub Desktop.
LeetCode Reverse Words in a String 2
import java.util.*;
public class ReverseWordsinaStringII {
public void reverseWords(char[] s) {
int start = 0;
int end = 0;
for (int i = 0; i < s.length; i++) {
if(s[i] == ' '){
end = i-1;
reverse(s, start, end);
start = i+1;
}
}
reverse(s, start, s.length-1);
reverse(s, 0, s.length-1);
}
public void reverse(char[] s, int start, int end){
while(start < end){
char tmp = s[end];
s[end] = s[start];
s[start] = tmp;
start++;
end--;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment