-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReverseWordsInPlace.java
54 lines (45 loc) · 1.06 KB
/
ReverseWordsInPlace.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* daily coding problem #113 */
/* Given a string of words delimited by spaces,
* reverse the words in string. For example,
* given "hello world here", return "here world hello"
* no extra space allowed
*/
public class ReverseWordsInPlace
{
public static void main(String[] args)
{
char[] arr = new char[]{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
reverseWords(arr);
System.out.println(String.valueOf(arr));
}
public static void reverseWords(char[] arr)
{
int i = 0;
// reverse the char sequence of every single word
// hello world --> olleh dlrow
for(int j = 0; j < arr.length; j++)
{
if(arr[j] == ' ')
{
reverse(arr, i, j-1);
i = j+1;
}
}
// perform reverse for the last word
reverse(arr, i, arr.length-1);
// reverse the entire char array
// example: olleh dlrow --> world hello
reverse(arr, 0, arr.length-1);
}
public static void reverse(char[] arr, int start, int end)
{
while(start < end)
{
char temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
}