-
Notifications
You must be signed in to change notification settings - Fork 19
/
ReverseString.java
55 lines (47 loc) · 1.1 KB
/
ReverseString.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
package oj.leetcode;
public class ReverseString {
public String reverseWords(String s) {
s = s.trim(); // imit the head and tail blanks
int i = 0;
int count = 0;
StringBuffer sb = new StringBuffer();
// imit the inter multi space
while (i < s.length()) {
if (s.charAt(i) != ' ') {
if (count > 1)
count = 0;
sb.append(s.charAt(i));
} else {
count++;
if (count == 1)
sb.append(' ');
}
i++;
}
s = reverseWord((new String(sb)));
StringBuffer res = new StringBuffer();
i = 0;
for (int j = i; j < s.length(); j++) {
if (s.charAt(j) == ' ') {
// reverse the word
res.append(s.substring(i, j));
i = j + 1;
}
//
}
return res.toString();
}
public String reverseWord(String s) {
char[] cs = s.toCharArray(); // can use replace()
for (int i = 0; i < cs.length / 2; i++) {
char temp = cs[i];
cs[i] = cs[cs.length - i - 1];
cs[cs.length - i - 1] = temp;
}
return new String(cs);
}
public static void main(String[] args) {
ReverseString rs = new ReverseString();
System.out.println(rs.reverseWords(" von zho "));
}
}