|
1 | 1 | package com.fishercoder.solutions; |
2 | 2 |
|
3 | | -import com.fishercoder.common.utils.CommonUtils; |
4 | | - |
5 | 3 | import java.util.ArrayDeque; |
6 | 4 | import java.util.Deque; |
7 | 5 |
|
8 | 6 | /** |
9 | 7 | * 151. Reverse Words in a String |
10 | | - * Given an input string, reverse the string word by word. |
11 | | -For example, |
12 | | -Given s = "the sky is blue", |
13 | | -return "blue is sky the". |
| 8 | +
|
| 9 | + Given an input string, reverse the string word by word. |
| 10 | + For example, |
| 11 | + Given s = "the sky is blue", |
| 12 | + return "blue is sky the". |
14 | 13 |
|
15 | 14 | Clarification: |
| 15 | +
|
16 | 16 | What constitutes a word? |
17 | 17 | A sequence of non-space characters constitutes a word. |
18 | 18 | Could the input string contain leading or trailing spaces? |
19 | 19 | Yes. However, your reversed string should not contain leading or trailing spaces. |
20 | 20 | How about multiple spaces between two words? |
21 | 21 | Reduce them to a single space in the reversed string. |
22 | | -
|
23 | 22 | */ |
24 | 23 |
|
25 | 24 | public class _151 { |
26 | | - |
| 25 | + public static class Solution1 { |
27 | 26 | public String reverseWords(String s) { |
28 | | - s.trim(); |
29 | | - if (s == null || s.length() == 0) { |
30 | | - return ""; |
| 27 | + s.trim(); |
| 28 | + if (s == null || s.length() == 0) { |
| 29 | + return ""; |
| 30 | + } |
| 31 | + String[] words = s.split(" "); |
| 32 | + if (words == null || words.length == 0) { |
| 33 | + return ""; |
| 34 | + } |
| 35 | + Deque<String> stack = new ArrayDeque<>(); |
| 36 | + for (String word : words) { |
| 37 | + if (!word.equals("")) { |
| 38 | + stack.offer(word); |
31 | 39 | } |
32 | | - String[] words = s.split(" "); |
33 | | - if (words == null || words.length == 0) { |
34 | | - return ""; |
35 | | - } |
36 | | - Deque<String> stack = new ArrayDeque<>(); |
37 | | - for (String word : words) { |
38 | | - if (!word.equals("")) { |
39 | | - stack.offer(word); |
40 | | - } |
41 | | - } |
42 | | - StringBuilder stringBuilder = new StringBuilder(); |
43 | | - while (!stack.isEmpty()) { |
44 | | - stringBuilder.append(stack.pollLast()).append(" "); |
45 | | - } |
46 | | - return stringBuilder.substring(0, stringBuilder.length() - 1).toString(); |
47 | | - } |
48 | | - |
49 | | - public static void main(String... args) { |
50 | | - /**This main program is to demo: |
51 | | - * a string that contains consecutive empty spaces when splitting by delimiter " ", |
52 | | - * it'll produce a an "" as an element.*/ |
53 | | - String s = "a b c"; |
54 | | - String[] strs = s.split(" "); |
55 | | - CommonUtils.printArray_generic_type(strs); |
| 40 | + } |
| 41 | + StringBuilder stringBuilder = new StringBuilder(); |
| 42 | + while (!stack.isEmpty()) { |
| 43 | + stringBuilder.append(stack.pollLast()).append(" "); |
| 44 | + } |
| 45 | + return stringBuilder.substring(0, stringBuilder.length() - 1).toString(); |
56 | 46 | } |
| 47 | + } |
57 | 48 | } |
0 commit comments