-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-186-Reverse-Words-in-a-String-II.java
More file actions
93 lines (79 loc) · 2.32 KB
/
LeetCode-186-Reverse-Words-in-a-String-II.java
File metadata and controls
93 lines (79 loc) · 2.32 KB
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
92
93
/*
LeetCode: https://leetcode.com/problems/reverse-words-in-a-string-ii/
LintCode:
JiuZhang:
ProgramCreek:
Analysis:
1.Reverse the whole array
2.Reverse the subarray seperated by ' '
Be careful, the last subarray should also be reversed. For example, "hi!" should be reversed in second step.
*/
public class Solution {
// 1.
public void reverseWords(char[] s) {
if(s == null || s.length <= 1)return;
reverseWords(s, 0, s.length - 1);
int last = 0;
for(int i = 0; i <= s.length; i++){
if(i == s.length || s[i] == ' '){ // be careful about the last subarray
reverseWords(s, last, i - 1);
last = i + 1;
}
}
}
private void reverseWords(char[] s, int lo, int hi){
while(lo < hi){
char temp = s[lo];
s[lo] = s[hi];
s[hi] = temp;
lo++;
hi--;
}
}
// 2.
// public void reverseWords(char[] str) {
// int begin = 0, end = 0;
// while (end < str.length) {
// if (str[end] == ' ') {
// reverse(str, begin, end - 1);
// begin = end + 1;
// end = begin;
// } else {
// end++;
// }
// }
// if (begin < end) reverse(str, begin, end - 1);
// reverse(str, 0, str.length - 1);
// }
// private void reverse(char[] str, int i, int j) {
// while(i < j) {
// char temp = str[i];
// str[i] = str[j];
// str[j] = temp;
// i++;
// j--;
// }
// }
// 3
public void reverseWords(char[] s) {
int begin = 0, end = 0;
while (end < s.length) {
while (end < s.length && s[end] != ' ') end++;
if (begin != end) {
reverse(s, begin, end - 1);
}
while (end < s.length && s[end] == ' ') end++;
begin = end;
}
reverse(s, 0, s.length - 1);
}
private void reverse(char[] s, int l, int r) {
while (l < r) {
char temp = s[l];
s[l] = s[r];
s[r] = temp;
l++;
r--;
}
}
}