Description Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example Input: “Let’s take LeetCode contest” Output: “s’teL ekat edoCteeL tsetnoc”
Note In the string, each word is separated by single space and there will not be any extra space in the string.
Solution
publicString reverseWords(String s) {
String[] temp = s.split(" ");
String result = "";
for (int i = 0; i < temp.length; i++) {
StringBuffer sb = new StringBuffer(temp[i]);
temp[i] = sb.reverse().toString();
result += temp[i]+" ";
}
System.out.println(result.trim());
return result.trim();
}
数据结构与算法:leetcode_557_Reverse Words in a String III
Description Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.Example Input: “Let’s take LeetCode ...