-
Notifications
You must be signed in to change notification settings - Fork 1
/
151.reverse-words-in-a-string.java
91 lines (88 loc) · 2.35 KB
/
151.reverse-words-in-a-string.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* @lc app=leetcode id=151 lang=java
*
* [151] Reverse Words in a String
*/
// @lc code=start
class Solution {
public String reverseWords(String s) {
s = s.trim();
char[] c = s.toCharArray();
char[] reverseWords = reverseWord(c);
String str = new String(reverseWords);
String[] parts = str.split("\\s+");
String ress = "";
int flag = 0;
for (String part : parts) {
char[] ca = part.toCharArray();
char[] res = reverseWord(ca);
String strr = new String(res);
if (flag == 0) {
ress += strr;
flag = 1;
} else {
ress += " " + strr;
}
}
return ress;
}
private char[] reverseWord(char[] c) {
int i = 0;
int j = c.length - 1;
while (i < j) {
char temp = c[i];
c[i] = c[j];
c[j] = temp;
i++;
j--;
}
return c;
}
}
// @lc code=end
/*
class Solution {
public String reverseWords(String s) {
StringBuilder sb = removeSpace(s);
reverseString(sb, 0, sb.length() - 1);
reverseEachWord(sb);
return sb.toString();
}
private StringBuilder removeSpace(String s) {
int start = 0;
int end = s.length() - 1;
while (s.charAt(start) == ' ') start++;
while (s.charAt(end) == ' ') end--;
StringBuilder sb = new StringBuilder();
while (start <= end) {
char c = s.charAt(start);
if (c != ' ' || sb.charAt(sb.length() - 1) != ' ') {
sb.append(c);
}
start++;
}
return sb;
}
public void reverseString(StringBuilder sb, int start, int end) {
while (start < end) {
char temp = sb.charAt(start);
sb.setCharAt(start, sb.charAt(end));
sb.setCharAt(end, temp);
start++;
end--;
}
}
private void reverseEachWord(StringBuilder sb) {
int start = 0;
int end = 1;
int n = sb.length();
while (start < n) {
while (end < n && sb.charAt(end) != ' ') {
end++;
}
reverseString(sb, start, end - 1);
start = end + 1;
end = start + 1;
}
}
} */