Skip to content

Commit 9ea9c21

Browse files
refactor 151
1 parent aaaf19a commit 9ea9c21

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ Your ideas/fixes/algorithms are more than welcome!
452452
|154|[Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_154.java)| O(logn)|O(1) | Hard| Array, Binary Search
453453
|153|[Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_153.java)| O(logn)|O(1) | Medium| Array, Binary Search
454454
|152|[Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_152.java)| O(n)|O(1) | Medium| Array
455-
|151|[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_151.java)| O(n)|O(n) | Medium|
455+
|151|[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_151.java)| O(n)|O(n) | Medium| String
456456
|150|[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_150.java)| O(?)|O(?) | Medium
457457
|149|[Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_149.java)| O(?)|O(?) | Hard|
458458
|148|[Sort List](https://leetcode.com/problems/sort-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_148.java) O(nlogn)|O(h) | Medium| Linked List, Sort
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,53 @@
11
package com.fishercoder.solutions;
2-
/**Given an input string, reverse the string word by word.
32

3+
import com.fishercoder.common.utils.CommonUtils;
4+
5+
import java.util.ArrayDeque;
6+
import java.util.Deque;
7+
8+
/**
9+
* 151. Reverse Words in a String
10+
* Given an input string, reverse the string word by word.
411
For example,
512
Given s = "the sky is blue",
6-
return "blue is sky the".*/
13+
return "blue is sky the".
14+
15+
Clarification:
16+
What constitutes a word?
17+
A sequence of non-space characters constitutes a word.
18+
Could the input string contain leading or trailing spaces?
19+
Yes. However, your reversed string should not contain leading or trailing spaces.
20+
How about multiple spaces between two words?
21+
Reduce them to a single space in the reversed string.
22+
23+
*/
724

825
public class _151 {
26+
927
public String reverseWords(String s) {
10-
if(!s.contains(" ")) return s;//for cases like this: "a"
11-
if(s.matches(" *")) return "";//for cases like this: " "
28+
s.trim();
29+
if (s == null || s.length() == 0) return "";
1230
String[] words = s.split(" ");
13-
StringBuilder stringBuilder = new StringBuilder();
14-
for(int i = words.length-1; i >= 0; i--){
15-
if(!words[i].equals("") && !words[i].equals(" ")){
16-
stringBuilder.append(words[i]);
17-
stringBuilder.append(" ");
31+
if (words == null || words.length == 0) return "";
32+
Deque<String> stack = new ArrayDeque<>();
33+
for (String word : words) {
34+
if (!word.equals("")) {
35+
stack.offer(word);
1836
}
1937
}
20-
stringBuilder.deleteCharAt(stringBuilder.length()-1);
21-
return stringBuilder.toString();
38+
StringBuilder stringBuilder = new StringBuilder();
39+
while (!stack.isEmpty()) {
40+
stringBuilder.append(stack.pollLast()).append(" ");
41+
}
42+
return stringBuilder.substring(0, stringBuilder.length()-1).toString();
43+
}
44+
45+
public static void main(String... args) {
46+
/**This main program is to demo:
47+
* a string that contains consecutive empty spaces when splitting by delimiter " ",
48+
* it'll produce a an "" as an element.*/
49+
String s = "a b c";
50+
String[] strs = s.split(" ");
51+
CommonUtils.printArray_generic_type(strs);
2252
}
2353
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._151;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _151Test {
10+
private static _151 test;
11+
private static String s;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
test = new _151();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
s = " ";
21+
assertEquals("", test.reverseWords(s));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
s = " 1";
27+
assertEquals("1", test.reverseWords(s));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
s = " a b ";
33+
assertEquals("b a", test.reverseWords(s));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)