-
Notifications
You must be signed in to change notification settings - Fork 820
/
ReverseWordsII.java
66 lines (64 loc) · 1.63 KB
/
ReverseWordsII.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
55
56
57
58
59
60
61
62
63
64
65
66
/* (C) 2024 YourCompanyName */
package string;
/**
* Created by gouthamvidyapradhan on 21/03/2017. Given an input string, reverse the string word by
* word. A word is defined as a sequence of non-space characters.
*
* <p>The input string does not contain leading or trailing spaces and the words are always
* separated by a single space.
*
* <p>For example, Given s = "the sky is blue", return "blue is sky the".
*
* <p>Could you do it in-place without allocating extra space?
*/
public class ReverseWordsII {
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
char[] c = {'t', 'h', 'e', ' ', 's', 'k', 'y', ' ', 'i', 's', ' ', 'b', 'l', 'u', 'e'};
new ReverseWordsII().reverseWords(c);
for (char i : c) System.out.print(i);
}
public void reverseWords(char[] s) {
for (int i = 0, j = s.length - 1; i < j; i++, j--) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
for (int i = 0, j = 0, l = s.length; i < l; ) {
if (s[i] == ' ') {
if (s[i - 1] == ' ') {
j = i;
i++;
j++;
} else {
i = i - 1;
for (int p = j, q = i; p < q; p++, q--) {
char temp = s[p];
s[p] = s[q];
s[q] = temp;
}
i = i + 1;
j = i;
i++;
j++;
}
} else if (i == l - 1) {
for (int p = j, q = i; p < q; p++, q--) {
char temp = s[p];
s[p] = s[q];
s[q] = temp;
}
j = i;
i++;
j++;
} else {
i++;
}
}
}
}