151 Reverse Words in a String – Medium
Problem:
Thoughts:
Solutions:
public class Solution {
public String reverseWords(String s) {
if (s == null)
return null;
s = s.trim();
String result = "";
String word = "";
for (int i = 0; i < s.length(); i ++){
if (s.charAt(i) == ' '){
while (s.charAt(i) == ' ')
i ++;
result = " " + word + result;
word = "" + s.charAt(i);
}
else{
word +=s.charAt(i);
}
}
result = word + result;
return result;
}
}Last updated