File tree Expand file tree Collapse file tree 3 files changed +21
-4
lines changed Expand file tree Collapse file tree 3 files changed +21
-4
lines changed Original file line number Diff line number Diff line change 134134| 147 | [ Insertion Sort List] ( https://leetcode.com/problems/insertion-sort-list ) | [ ![ Java] ( assets/java.png )] ( src/InsertionSortList.java ) | |
135135| 148 | [ Sort List] ( https://leetcode.com/problems/sort-list ) | [ ![ Java] ( assets/java.png )] ( src/SortList.java ) | |
136136| 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 ) | |
138138| 152 | [ Maximum Product Subarray] ( https://leetcode.com/problems/maximum-product-subarray ) | | |
139139| 153 | [ Find Minimum in Rotated Sorted Array] ( https://leetcode.com/problems/find-minimum-in-rotated-sorted-array ) | | |
140140| 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 11public class HelloWorld {
22 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 " ));
64 }
75}
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