-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseWordsinaStringIII.java
More file actions
62 lines (60 loc) · 2.02 KB
/
ReverseWordsinaStringIII.java
File metadata and controls
62 lines (60 loc) · 2.02 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
package easy;
import java.util.Stack;
/**
*
* ClassName: ReverseWordsinaStringIII
* @author chenyiAlone
* Create Time: 2019/03/03 21:19:16
* Description: No.557
* Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
*/
public class ReverseWordsinaStringIII {
public String reverseWords(String s) {
char[] arr = s.toCharArray();
int start = 0, len = s.length();
while (start < len) {
int end = s.indexOf(' ', start);
if (end == -1) {
hander(arr, start, len - 1);
break;
} else {
hander(arr, start, end - 1);
start = end + 1;
}
}
return String.valueOf(arr);
}
public static void hander(char[] arr, int start, int end) {
while (start < end) {
char tmp = arr[start];
arr[start++] = arr[end];
arr[end--] = tmp;
}
}
public String reverseWordsStack(String s) {
Stack<Character> stack = new Stack<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' ') {
stack.push(s.charAt(i));
} else {
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
sb.append(s.charAt(i));
}
}
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
return sb.toString();
}
public static void main(String[] args) {
String s = "Let's take LeetCode contest";
System.out.println(new ReverseWordsinaStringIII().reverseWords(s));
}
}