-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseWordsInString.java
71 lines (65 loc) · 2.38 KB
/
ReverseWordsInString.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
class Solution {
public String reverseWords(String s) {
StringBuilder words = new StringBuilder();
char[] sArr = s.toCharArray();
int firstIdx = 0;
for (int i = 0; i < sArr.length; i++) {
char c = sArr[i];
if (c == ' ') {
reverse(sArr, firstIdx, i-1);
firstIdx = i + 1;
}
}
reverse(sArr, firstIdx, sArr.length - 1);
return new String(sArr);
}
public void reverse(char[] sArr, int firstIdx, int lastIdx) {
while (firstIdx < lastIdx) {
char temp = sArr[firstIdx];
sArr[firstIdx++] = sArr[lastIdx];
sArr[lastIdx--] = temp;
}
}
// public String reverseWords(String s) {
// Stack<String> reverseWords = new Stack<String>();
// StringBuilder words = new StringBuilder();
// for (int i = s.length()-1; i >= 0; i--) {
// char c = s.charAt(i);
// if (c == ' ') {
// reverseWords.push(words.toString());
// words.setLength(0);
// }
// else words.append(c);
// }
// while (!reverseWords.empty()) {
// words.append(" ").append(reverseWords.pop());
// }
// return words.toString();
// }
// public String reverseWords(String s) {
// String[] words = s.split(" ");
// StringBuilder reversedStr = new StringBuilder();
// for (int i = 0; i < words.length; i++) {
// StringBuilder word = new StringBuilder(words[i]);
// if (i != 0) reversedStr.append(" ");
// reversedStr.append(word.reverse());
// }
// return reversedStr.toString();
// }
// public String reverseWords(String s) {
// char[] sArr = s.toCharArray();
// StringBuilder reversedStr = new StringBuilder();
// StringBuilder reversedWord = new StringBuilder();
// for (int i = 0; i < sArr.length; i++) {
// char c = sArr[i];
// if (c == ' ') {
// reversedStr.append(reversedWord.reverse()).append(' ');
// reversedWord.setLength(0);
// }
// else
// reversedWord.append(c);
// }
// reversedStr.append(reversedWord.reverse());
// return reversedStr.toString();
// }
}