557. Reverse Words in a String III
原创
©著作权归作者所有:来自51CTO博客作者wx62ea2466cca9a的原创作品,请联系作者获取转载授权,否则将追究法律责任
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.
public class Solution {
public String reverseWords(String s) {
if(s.length() == 0) return s;
String[] words = s.split(" ");
StringBuffer sb = new StringBuffer("");
for(String word : words){
for(int i = word.length() - 1 ;i >= 0; i --) sb.append(word.charAt(i));
sb.append(" ");
}
sb.setLength(sb.length() - 1);
return