LeetCode – Reverse Words in a String (Java)
Given an input string, reverse the string word by word.
For example, given s = "the sky is blue", return "blue is sky the".
Java Solution
This problem is pretty straightforward. We first split the string to words array, and then iterate through the array and add each element to a new string. Note: StringBuilder should be used to avoid creating too many Strings. If the string is very long, using String is not scalable since String is immutable and too many objects will be created and garbage collected.
class Solution { public String reverseWords(String s) { if (s == null || s.length() == 0) { return ""; } // split to words by space String[] arr = s.split(" "); StringBuilder sb = new StringBuilder(); for (int i = arr.length - 1; i >= 0; --i) { if (!arr[i].equals("")) { sb.append(arr[i]).append(" "); } } return sb.length() == 0 ? "" : sb.substring(0, sb.length() - 1); } } |
<pre><code> String foo = "bar"; </code></pre>
-
Berns Dela Vega
-
R M
-
R M
-
Anuj Kumar
-
STM
-
melad
-
Melad
-
Onkar
-
Renzo
-
Dolly Agarwal
-
Bhavani
-
sonia
-
Sushant Raj
-
daksh
-
Taoufik Bdiri
-
varun
-
Jared Burrows
-
henrik
-
Henrik
-
Souraf khan
-
Tiago Pinho
-
RK
-
Steve
-
MJCS
-
MJCS
-
on_fla
-
kiran
-
muralikrishna Adusumalli
-
ramesh
-
Farah El Uasghiri
-
Guest
-
Guest
-
Cheng
-
Taras