File tree 3 files changed +21
-4
lines changed
3 files changed +21
-4
lines changed Original file line number Diff line number Diff line change 134
134
| 147 | [ Insertion Sort List] ( https://leetcode.com/problems/insertion-sort-list ) | [ ![ Java] ( assets/java.png )] ( src/InsertionSortList.java ) | |
135
135
| 148 | [ Sort List] ( https://leetcode.com/problems/sort-list ) | [ ![ Java] ( assets/java.png )] ( src/SortList.java ) | |
136
136
| 150 | [ Evaluate Reverse Polish Notation] ( https://leetcode.com/problems/evaluate-reverse-polish-notation ) | [ ![ Java] ( assets/java.png )] ( src/EvaluateReversePolishNotation.java ) | |
137
- | 151 | [ Reverse Words in a String] ( https://leetcode.com/problems/reverse-words-in-a-string ) | | |
137
+ | 151 | [ Reverse Words in a String] ( https://leetcode.com/problems/reverse-words-in-a-string ) | [ ![ Java ] ( assets/java.png )] ( src/ReverseWordsInAString.java ) | |
138
138
| 152 | [ Maximum Product Subarray] ( https://leetcode.com/problems/maximum-product-subarray ) | | |
139
139
| 153 | [ Find Minimum in Rotated Sorted Array] ( https://leetcode.com/problems/find-minimum-in-rotated-sorted-array ) | | |
140
140
| 155 | [ Min Stack] ( https://leetcode.com/problems/min-stack ) | [ ![ Java] ( assets/java.png )] ( src/MinStack.java ) [ ![ Python] ( assets/python.png )] ( python/min_stack.py ) | |
Original file line number Diff line number Diff line change 1
1
public class HelloWorld {
2
2
public static void main (String [] args ) {
3
- System .out .println (Integer .parseInt ("2" ));
4
- System .out .println (Integer .parseInt ("-11" ));
5
- System .out .println (EvaluateReversePolishNotation .evalRPN (new String [] {"2" , "-11" , "-" }));
3
+ System .out .println (ReverseWordsInAString .reverseWords (" hello world " ));
6
4
}
7
5
}
Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/reverse-words-in-a-string
2
+ // T: O(s)
3
+ // S: O(s)
4
+
5
+ public class ReverseWordsInAString {
6
+ private static final char SPACE = ' ' ;
7
+
8
+ public static String reverseWords (String s ) {
9
+ final String [] words = s .split (" " );
10
+ final StringBuilder result = new StringBuilder ();
11
+ for (int i = words .length - 1 ; i >= 0 ; i --) {
12
+ String word = words [i ];
13
+ if (word .isEmpty ()) continue ;
14
+ result .append (word ).append (SPACE );
15
+ }
16
+ result .deleteCharAt (result .length () - 1 );
17
+ return result .toString ();
18
+ }
19
+ }
You can’t perform that action at this time.
0 commit comments