题目描述:
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 1:
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.
Subscribe to see which companies asked this question.
Show Similar Problems
题目链接:点击打开链接
简单题,思路主要就是把这个句子每个单词分开,存进数组,然后每个单词分别反转就好了,StringBuffer,StringBuilder随你选,一样的。
public static String reverseWords(String s) {
StringBuffer s1=new StringBuffer();
String []ss=s.split(" ");
for (int i = 0; i < ss.length; i++) s1 = s1.append(new StringBuffer(ss[i]).reverse().append(" "));
s1.deleteCharAt(s1.length()-1);
return s1.toString();
}