Last active
August 29, 2015 14:27
-
-
Save xynophon/9507d51a3d1f33242d59 to your computer and use it in GitHub Desktop.
LeetCode Reverse Words in a String 2
This file contains hidden or 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
| 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